【HDU4725】【建模dij】【新建虚拟节点 每层一个出口和入口】

来源:互联网 发布:大数据专业 编辑:程序博客网 时间:2024/05/19 00:48

The Shortest Path in Nya Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3874    Accepted Submission(s): 911


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
23 3 31 3 21 2 12 3 11 3 33 3 31 3 21 2 22 3 21 3 4
 

Sample Output
Case #1: 2Case #2: 3
 

Source
2013 ACM/ICPC Asia Regional Online —— Warmup2
 

Recommend
zhuyuanchen520




#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <cstdlib>#include <cstdio>#include <algorithm>using namespace std;    #define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define mp push_backint T;int n,m,c;int H[100010*3];struct Edge{int v,c,next;}edge[100010*6+100010];int top;int d[100010*3];bool ok[100010*3];void addedge(int u,int v,int c){edge[top] = {v,c,H[u]};H[u] = top++;}struct Qnode{int idx,dis;Qnode(){};Qnode(int _i,int _d):idx(_i),dis(_d){};bool operator<(const Qnode&x) const{return  dis > x.dis;}};// +n 是入口 +2n是出口int dij(int st){for(int i=1;i<=3*n;i++){ok[i] = false;d[i] = 0x3f3f3f3f;}d[st] = 0;priority_queue<Qnode> pq;pq.push(Qnode(st,0));while(!pq.empty()){Qnode cur = pq.top();pq.pop();if(ok[cur.idx]) continue;ok[cur.idx] = true;for(int ei=H[cur.idx];ei!=-1;ei=edge[ei].next){Edge e = edge[ei];if(!ok[e.v] && d[e.v] > d[cur.idx] + e.c){d[e.v] = d[cur.idx] + e.c;pq.push(Qnode(e.v,d[e.v]));}}}return d[n] == 0x3f3f3f3f ? -1:d[n];}int main(){    scanf("%d",&T);int C = 1;while(T--){scanf("%d%d%d",&n,&m,&c);top = 0;memset(H,-1,sizeof(H));for(int i=1;i<=n;i++){int l;scanf("%d",&l);addedge(n+l,i,0);addedge(i,2*n+l,0);}for(int i=2;i<=n;i++){addedge(i-1+2*n,i+n,c);addedge(i+2*n,i-1+n,c);}for(int i=1;i<=m;i++){int u,v,c;scanf("%d%d%d",&u,&v,&c);addedge(u,v,c);addedge(v,u,c);}printf("Case #%d: %d\n",C++,dij(1));}    return 0;}


0 0
原创粉丝点击