POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
Posted 太子丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。相关的知识,希望对你有一定的参考价值。
POJ 3268 Silver Cow Party
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow‘s return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10
Hint
1 #include <set>
2 #include <map>
3 #include <stack>
4 #include <stdio.h>
5 #include <vector>
6 #include <utility>
7 #include<string.h>
8 #include <queue>
9 #include <iterator>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 int inf = 0x3f3f3f3f;
16 int n,m,x;
17 struct edge
18 {
19 int u;
20 int v;
21 int cost;
22 } M[100050]; //存储原始边的信息;
23 struct N
24 {
25 int e; //每条边的权值;
26 int to; //终点;
27 };
28 vector<N> v[1200]; // 存储每条边的信息,下标为起始点;
29 int dis[1200]; // 起点到各个终点的最短距离;
30 void dijkstra(int s)
31 {
32 priority_queue<pair<int ,int>,vector<pair<int,int> >,greater<pair<int,int> > >q; //利用优先队列对里面的边从小到大进行排序;
33 for(int i = 1;i<=1199;i++) v[i].clear(); //vector 清空;
34 fill(dis,dis+1200,inf); //初始化为最大值;
35 N n2;
36 for(int i = 1;i<=m;i++) //将初始边加入到vector中;
37 {
38 n2.e = M[i].cost;
39 n2.to = M[i].v;
40 v[M[i].u].push_back(n2);
41 }
42 dis[s] = 0;
43 //优化与实现:
44 q.push(pair<int,int>(0,s));
45 while(!q.empty())
46 {
47 pair<int ,int> p = q.top();
48 q.pop();
49 int v1 = p.second;
50 if(dis[v1]<p.first) continue;
51 for(int i = 0;i<v[v1].size();i++)
52 {
53 N n1 = v[v1][i];
54 if(dis[n1.to]>dis[v1]+n1.e)
55 {
56 dis[n1.to]=dis[v1]+n1.e;
57 q.push(pair<int,int>(dis[n1.to],n1.to));
58 }
59 }
60 }
61 }
62 int main()
63 {
64 int sum[1200];
65 while(~scanf("%d %d %d",&n,&m,&x))
66 {
67 memset(sum,0,sizeof(sum));
68 for(int i = 1;i<=m;i++) scanf("%d %d %d",&M[i].u,&M[i].v,&M[i].cost);
69 dijkstra(x);
70 for(int i = 1;i<=n;i++) sum[i]+=dis[i];
71 for(int i = 1;i<=n;i++)
72 {
73 dijkstra(i);
74 sum[i] += dis[x];
75 }
76 int max1 = 0;
77 for(int i = 1;i<=n;i++) if(sum[i]>max1) max1 = sum[i];
78 printf("%d\n",max1);
79 }
80 return 0;
81 }
本文为个人随笔,如有不当之处,望各位大佬多多指教.
若能为各位博友提供小小帮助,不胜荣幸.
以上是关于POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。的主要内容,如果未能解决你的问题,请参考以下文章
POJ 3268 Silver Cow Party (Dijkstra)