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 ≤ XN). 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

Line 1: Three space-separated integers, respectively: N, M, and X
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

Line 1: One integer: the maximum of time any one cow must walk.

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

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

题目描述:

一群牛分别从1~n号农场赶往x号农场参加聚会,农场与农场之间的路是单向的,在n个农场之间有m条路,给出 a ,b , t表示从a号农场到b号农场需要t时间。 每头牛都会选择最短的路,问来回路上花费时间最长的牛花费的时间是多少。

思路描述:

题意并不难理解,即求农场 X 到其余农场的最短距离与其余农场到X农场的最短距离,农场 X 到其余农场的最短距离一次dijkstra便可得到,但是求其余农场到X农场的最短距离,需要n次dijkstra方可得到,观察数据量便可得知普通的dijkstra算法难以满足要求,因为普通的dijkstra算法时间复杂度为O(n^2),加之n+1次调用,根据数据量必定超时,所以下面代码便对普通的dijkstra算法进行了优化,使之时间复杂度变为O(n*n*log n),便可满足题目的要求。而主要的实现是通过优先队列对边进行排序,从而先去了每次寻找最小边所花费的时间,具体细节详见代码如下:

代码实现:

 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 }
本文为个人随笔,如有不当之处,望各位大佬多多指教.若能为各位博友提供小小帮助,不胜荣幸.
时间: 2024-08-24 06:14:44

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。的相关文章

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

poj 3268 Silver Cow Party(最短路)

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

POJ 3268 Silver Cow Party(SPFA)

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 re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 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 

poj 3268 Silver Cow Party 【最短路Dijkstra + 结构体妙用】

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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 (

POJ 3268 Silver Cow Party(最短路dijkstra)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15451   Accepted: 6997 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

POJ 3268 Silver Cow Party (来回最短路 SPFA)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14384   Accepted: 6490 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 ≤

POJ 3268 Silver Cow Party (Dijkstra)

题目链接:POJ 3268 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