博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu4725——The Shortest Path in Nya Graph(SPFA+两层图)
阅读量:2343 次
发布时间:2019-05-10

本文共 2496 字,大约阅读时间需要 8 分钟。

Problem Description

This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.
The Nya graph is an undirected graph with “layers”. Each node in the graph belongs to a layer, there are N nodes in total.
You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost.
Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
Help us calculate the shortest path from node 1 to node N.

Input

The first line has a number T (T <= 20) , indicating the number of test cases.
For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w.

Output

For test case X, output “Case #X: ” first, then output the minimum cost moving from node 1 to node N.
If there are no solutions, output -1.

Sample Input

2
3 3 3
1 3 2
1 2 1
2 3 1
1 3 3

3 3 3

1 3 2
1 2 2
2 3 2
1 3 4

Sample Output

Case #1: 2
Case #2: 3

挺有意思的最短路

题意是在无向图中,每个节点还属于一个层(layer),可以从第i个层的节点移动到第i+1个层的节点,花费是c。然后是给出一个普通的无向图,求1到n的最短路。
因为有层的存在,在寻找最短路的时候可能直接经过层,不妨把层也当做节点,层与所属节点之间的花费是0,表示随时可以从层转移到节点上寻找最短路,然后层与层之间的花费是c,节点到下一层或上一层的的花费也是c,节点与节点之间的花费是w,方便起见,层的节点编号从n+1到n+n。
另外数组开大点,不然会TLE,我很奇怪为什么不是RE,害得我判断失误

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 200010#define Mod 10001using namespace std;struct Edge{ int v,w,next;};Edge edge[MAXN*20];int head[MAXN],n,m,e,vis[MAXN],dis[MAXN];void add(Edge *edge,int *head,int u,int v,int w){ edge[e].v=v; edge[e].w=w; edge[e].next=head[u]; head[u]=e; e++;}void spfa(Edge *edge,int *head,int u){ memset(vis,0,sizeof(vis)); for(int i=1; i<=2*n; ++i) //这里WA了 dis[i]=INF; dis[u]=0; queue
q; q.push(u); while(!q.empty()) { u=q.front(); q.pop(); vis[u]=0; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v,w=edge[i].w; if(w+dis[u]
1) add(edge,head,i,n+layer[i]-1,c); if(layer[i]

转载地址:http://ascvb.baihongyu.com/

你可能感兴趣的文章
对于定义"int *p",下列哪些说明可能是正确的?----腾讯2016研发工程师在线模拟笔试题
查看>>
华为研发工程师编程题----明明的随机数(快排)
查看>>
华为研发工程师编程题----进制转换(pow函数,string.find())
查看>>
华为研发工程师编程题----汽水瓶
查看>>
以下不属于tcp连接断开的状态是?----腾讯2016研发工程师笔试题
查看>>
下面关于ICMP协议的描述中,正确的是()----腾讯2016研发工程师笔试题
查看>>
对于移动平均算法,是计算某变量之前n个数值的算术平均,正确的说法是:----腾讯2016研发工程师在线模拟笔试题
查看>>
某一速率为100M的交换机有20个端口,其一个端口上连着一台笔记本电脑,此电脑从迅雷上下载一部1G的电影需要的时间可能是多久?
查看>>
在linux编程中,以下哪个TCP的套接字选项与nagle算法的开启和关闭有关?----腾讯2016研发工程师在线模拟笔试题
查看>>
某二叉树的先根遍历序列和后根遍历序列正好相反,则该二叉树具有的特征是()----腾讯2016研发工程师在线模拟笔试题
查看>>
win32系统里,下面几个sizeof的运行结果是()----腾讯2016研发工程师在线模拟笔试题
查看>>
若系统中有五台打印机,有多个进程均需要使用两台,规定每个进程一次仅允许申请一台,则在不发生死锁的情况下至多允许______个进程参与竞争
查看>>
关于红黑树和AVL树,以下哪种说法不正确?----腾讯2016研发工程师在线模拟笔试题
查看>>
关于epoll和select的区别,哪些说法是正确的?----腾讯2016研发工程师在线模拟笔试题
查看>>
以下是C++的不同数据类型值的比较语句,请问这些判断语句中作为条件部分的语句编写有问题的有:
查看>>
TCP链接中主动断开链接netstat观察可能出现的状态流转是:----腾讯2016研发工程师在线模拟笔试题
查看>>
以下涉及到内存管理的代码段中,有错误的是:----腾讯2016研发工程师在线模拟笔试题
查看>>
下面哪些特性可能导致代码体积膨胀:----腾讯2016研发工程师在线模拟笔试题
查看>>
const的使用方法----腾讯2016研发工程师笔试题(一)
查看>>
哪些设计模式是降低资源使用率:----腾讯2016研发工程师笔试题(一)
查看>>