HDU 4725 The Shortest Path in Nya Graph

he Shortest Path in Nya Graph

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on  HDU. Original ID: 4725
64-bit integer IO format: %I64d      Java class name: Main
 
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

Source

 
解题:
 
瞎搞搞。。
 
HDU 4725 The Shortest Path in Nya Graph
 1 #include <bits/stdc++.h>

 2 #define pii pair<int,int>

 3 using namespace std;

 4 const int INF = 0x3f3f3f3f;

 5 const int maxn = 200010;

 6 struct arc{

 7     int to,w,next;

 8     arc(int x = 0,int y = 0,int z = -1){

 9         to = x;

10         w = y;

11         next = z;

12     }

13 }e[1000000];

14 int head[maxn],d[maxn],tot,n,m,c;

15 int layer[maxn];

16 void add(int u,int v,int w){

17     e[tot] = arc(v,w,head[u]);

18     head[u] = tot++;

19 }

20 bool done[maxn];

21 priority_queue< pii,vector< pii >,greater< pii > >q;

22 int dijkstra(int s,int t){

23     while(!q.empty()) q.pop();

24     memset(d,0x3f,sizeof d);

25     memset(done,false,sizeof done);

26     q.push(pii(d[s] = 0,s));

27     while(!q.empty()){

28         int u = q.top().second;

29         q.pop();

30         if(done[u]) continue;

31         done[u] = true;

32         for(int i = head[u]; ~i; i = e[i].next){

33             if(d[e[i].to] > d[u] + e[i].w){

34                 d[e[i].to] = d[u] + e[i].w;

35                 q.push(pii(d[e[i].to],e[i].to));

36             }

37         }

38 

39     }

40     return d[t] == INF?-1:d[t];

41 }

42 bool hslv[maxn];

43 int main(){

44     int kase,tmp,u,v,w,cs = 1;

45     scanf("%d",&kase);

46     while(kase--){

47         memset(head,-1,sizeof head);

48         memset(hslv,false,sizeof hslv);

49         tot = 0;

50         scanf("%d%d%d",&n,&m,&c);

51         for(int i = 1; i <= n; ++i){

52             scanf("%d",&tmp);

53             layer[i] = tmp;

54             hslv[tmp] = true;

55         }

56         for(int i = 0; i < m; ++i){

57             scanf("%d%d%d",&u,&v,&w);

58             add(u,v,w);

59             add(v,u,w);

60         }

61         for(int i = 1; i <= n; ++i){

62             add(layer[i]+n,i,0);

63             if(layer[i] > 1) add(i,layer[i]-1+n,c);

64             if(layer[i] < n) add(i,layer[i]+n+1,c);

65         }

66         printf("Case #%d: %d\n",cs++,dijkstra(1,n));

67     }

68     return 0;

69 }
View Code

 

你可能感兴趣的:(Graph)