hlg1339Touring DIJ+堆优化
Description |
The best friends Mr. Li and Mr. Liu are touring in beautiful country M. M has n cities and m two-way roads in total. Each road connects two cities with fixed length.We assume that the cost of car traveling on the road is only related to the length of road,the longer road the more money to pay. Now,both Mr. Li and Mr. Liu are in the city C,they have chosen to travel separately by the next time. Mr. Li chooses city A with beautiful scenery to be next place, Mr. Liu goes to city B with ancient temples. You are their friend with clever minds,just tell them how to arrive the target places make total costs of them minimum. |
Input |
The input file contains sevearl test cases.The first line of each case are two positive integers n,and m(3<=n<=5000, 1<=m<=10000). The cities are named from 1 to n.Three positive integers C, A, B are follwing.Then,m lines are given,each line contains three integers i,j and k,indicating one road between i and j is exists,and should pay cost k by the car.
Process to the end of file. |
Output |
For each test case, first print a line saying "Scenario #p", where p is the number of the test case.Then,if both Mr. Li and Mr. Liu can manage to arrive their cities,output the minimum cost they will spend,otherwise output "Can not reah!", in one line.Print a blank line after each test case, even after the last one. |
Sample Input |
4 5 1 3 4 1 2 100 1 3 200 1 4 300 2 3 50 2 4 100 4 6 1 3 4 1 2 100 1 3 200 1 4 300 2 3 50 2 4 100 3 4 50 |
Sample Output |
Scenario #1 250 Scenario #2 200 |
Hint |
The car can carry with both of them at the same time. For case 1:Li and Liu can take car together from 1 to 2, and then Li can take car from 2 to 3,Liu can take car from 2 to 4,so the total cost is 100+50+100. |
题意:
给你一些点和一些无向边及其权值 然后给你三个点abc表示两人从a出发分别到b,c
问最少花费 如果两人同一时间有相同的路线的话那么花费为一个人的花费
分析:
我们可以考虑刚开始有一段路是两个人一起走的 花费为一份 到某个点之后 两人分开走 花费为两个人的花费之和
所以只要枚举每个点为那个店的花费 求出最小值即可
用DIJ算出abc分别到每个点的距离。。
代码:
1 #include <stdio.h> 2 #include <string.h> 3 #define NN 5050 4 #define LL(x) (x << 1) 5 #define RR(x) (x << 1 | 1) 6 #define PP(x) (x >> 1) 7 8 typedef struct _edge { 9 int v; 10 int w; 11 struct _edge * next; 12 }EDGE, *PEDGE; 13 14 const int INF = 0xfffffff; 15 PEDGE head[NN]; 16 EDGE edge[NN * 6]; 17 int pnt; 18 int nIndex[NN]; 19 int dist[NN]; 20 int q[NN]; 21 22 void addEdge(int u, int v, int w) 23 { 24 edge[pnt].v = v; 25 edge[pnt].w = w; 26 edge[pnt].next = head[u]; 27 head[u] = edge + pnt++; 28 } 29 30 void swap(int x, int y) 31 { 32 int t; 33 t = q[x]; 34 q[x] = q[y]; 35 q[y] = t; 36 nIndex[q[x]] = x; 37 nIndex[q[y]] = y; 38 } 39 40 void MinHeapify(int q[], int dist[], int i) 41 { 42 int min; 43 min = i; 44 if (LL(i) <= q[0] && dist[q[LL(i)]] < dist[q[min]]) min = LL(i); 45 if (RR(i) <= q[0] && dist[q[RR(i)]] < dist[q[min]]) min = RR(i); 46 if (min != i) { 47 swap(i, min); 48 MinHeapify(q, dist, min); 49 } 50 } 51 52 int PopHeap(int q[], int dist[]) 53 { 54 int min; 55 if (q[0] < 1) return -1; 56 min = q[1]; q[1] = q[q[0]]; 57 q[0]--; 58 nIndex[q[1]] = 1; 59 MinHeapify(q, dist, 1); 60 return min; 61 } 62 63 void UpdateHeap(int q[], int dist[], int rt) 64 { 65 while (PP(rt) && dist[q[rt]] < dist[q[PP(rt)]]) { 66 swap(rt, PP(rt)); 67 rt = PP(rt); 68 } 69 } 70 71 void BuildHeap(int q[], int dist[], int nNode) 72 { 73 int i; 74 q[0] = nNode; 75 for (i = 1; i <= q[0]; i++) { 76 q[i] = i; 77 nIndex[i] = i; 78 } 79 for (i = q[0] / 2; i >= 1; i--) { 80 MinHeapify(q, dist, i); 81 } 82 } 83 84 void Dijkstra(int nNode, int stNode, int dist[]) 85 { 86 int i, u, used[NN]; 87 PEDGE pd; 88 89 for (i = 1; i <= nNode; i++) { 90 dist[i] = INF; 91 used[i] = 0; 92 } 93 dist[stNode] = 0; 94 BuildHeap(q, dist, nNode); 95 while (q[0]) { 96 u = PopHeap(q, dist); 97 used[u] = 1; 98 for (pd = head[u]; pd; pd = pd->next) { 99 if (!used[pd->v] && dist[pd->v] > dist[u] + pd->w) { 100 dist[pd->v] = dist[u] + pd->w; 101 UpdateHeap(q, dist, nIndex[pd->v]); 102 } 103 } 104 } 105 } 106 107 int main() 108 { 109 int nNode, nEdge, i, u, v, w; 110 int nA,nB,nC; 111 int A[NN], B[NN], C[NN]; 112 int kase = 1; 113 freopen("ss.txt","r",stdin); 114 while (scanf("%d%d", &nNode, &nEdge) != EOF) { 115 scanf("%d %d %d",&nC,&nB,&nA); 116 pnt = 0; 117 memset(head, 0, sizeof(head)); 118 for (i = 0; i < nEdge; i++) { 119 scanf("%d%d%d", &u, &v, &w); 120 addEdge(u, v, w); 121 addEdge(v, u, w); 122 } 123 Dijkstra(nNode, nC, C); 124 Dijkstra(nNode, nB, B); 125 Dijkstra(nNode, nA, A); 126 long long int ans = INF; 127 for(int i = 1; i <= nNode; i++) 128 { 129 if(ans > A[i] + B[i] + C[i]) 130 ans = A[i] + B[i] + C[i]; 131 } 132 printf("Scenario #%d\n%lld\n\n",kase++,ans); 133 } 134 return 0; 135 }