【POJ】3268 - Silver Cow Party(dijkstra & 优先队列 & 好题)
2016-08-01 09:26
260 查看
点击打开题目
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
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
Sample Output
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.
Source
USACO 2007 February Silver
在比赛的时候一看要求双向距离,果断用 floyd 算法,结果和估计的一样,TLE。
然后又不想多次用 dijkstra ,最后居然放弃了(名次刷刷的往下掉)。
后来发现,这道题有个很巧妙的方法,不用对每一个点都进行最短路的计算,在计算 k 到其他点的时候,只需要翻转 mapp 矩阵即可(当然不用两个 for 循环来翻转,直接下标换一下位置就行了,感觉太巧妙了!)
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
int n,m,k;
struct node
{
int pos,v;
bool friend operator < (node a,node b)
{
return a.v > b.v;
}
}pr,ne;
int f[2][1011];
int mapp[1011][1011];
bool used[1011];
void dijkstra1()
{
CLR(used,false);
priority_queue<node> q;
pr.pos = k;
pr.v = 0;
q.push(pr);
while (!q.empty())
{
pr = q.top();
q.pop();
if (used[pr.pos])
continue;
used[pr.pos] = true;
f[0][pr.pos] = pr.v;
for (int i = 1 ; i <= n ; i++)
{
if (!used[i] && f[0][pr.pos] + mapp[pr.pos][i] < f[0][i])
{
ne.pos = i;
ne.v = f[0][pr.pos] + mapp[pr.pos][i];
q.push(ne);
}
}
}
}
void dijkstra2()
{
CLR(used,false);
priority_queue<node> q;
pr.pos = k;
pr.v = 0;
q.push(pr);
while (!q.empty())
{
pr = q.top();
q.pop();
if (used[pr.pos])
continue;
used[pr.pos] = true;
f[1][pr.pos] = pr.v;
for (int i = 1 ; i <= n ; i++)
{
if (!used[i] && f[1][pr.pos] + mapp[i][pr.pos] < f[1][i])
{
ne.pos = i;
ne.v = f[1][pr.pos] + mapp[i][pr.pos];
q.push(ne);
}
}
}
}
int main()
{
while (~scanf ("%d %d %d",&n,&m,&k))
{
CLR(mapp,INF);
CLR(f,INF);
for (int i = 1 ; i <= m ; i++)
{
int t1,t2,t3;
scanf ("%d %d %d",&t1,&t2,&t3);
mapp[t1][t2] = min(mapp[t1][t2] , t3);
}
dijkstra1();
dijkstra2();
int ans = 0;
for (int i = 1 ; i <= n ; i++)
ans = max (ans , f[0][i] + f[1][i]);
printf ("%d\n",ans);
}
return 0;
}
Silver Cow Party
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18390 | Accepted: 8414 |
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
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.
Source
USACO 2007 February Silver
在比赛的时候一看要求双向距离,果断用 floyd 算法,结果和估计的一样,TLE。
然后又不想多次用 dijkstra ,最后居然放弃了(名次刷刷的往下掉)。
后来发现,这道题有个很巧妙的方法,不用对每一个点都进行最短路的计算,在计算 k 到其他点的时候,只需要翻转 mapp 矩阵即可(当然不用两个 for 循环来翻转,直接下标换一下位置就行了,感觉太巧妙了!)
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
int n,m,k;
struct node
{
int pos,v;
bool friend operator < (node a,node b)
{
return a.v > b.v;
}
}pr,ne;
int f[2][1011];
int mapp[1011][1011];
bool used[1011];
void dijkstra1()
{
CLR(used,false);
priority_queue<node> q;
pr.pos = k;
pr.v = 0;
q.push(pr);
while (!q.empty())
{
pr = q.top();
q.pop();
if (used[pr.pos])
continue;
used[pr.pos] = true;
f[0][pr.pos] = pr.v;
for (int i = 1 ; i <= n ; i++)
{
if (!used[i] && f[0][pr.pos] + mapp[pr.pos][i] < f[0][i])
{
ne.pos = i;
ne.v = f[0][pr.pos] + mapp[pr.pos][i];
q.push(ne);
}
}
}
}
void dijkstra2()
{
CLR(used,false);
priority_queue<node> q;
pr.pos = k;
pr.v = 0;
q.push(pr);
while (!q.empty())
{
pr = q.top();
q.pop();
if (used[pr.pos])
continue;
used[pr.pos] = true;
f[1][pr.pos] = pr.v;
for (int i = 1 ; i <= n ; i++)
{
if (!used[i] && f[1][pr.pos] + mapp[i][pr.pos] < f[1][i])
{
ne.pos = i;
ne.v = f[1][pr.pos] + mapp[i][pr.pos];
q.push(ne);
}
}
}
}
int main()
{
while (~scanf ("%d %d %d",&n,&m,&k))
{
CLR(mapp,INF);
CLR(f,INF);
for (int i = 1 ; i <= m ; i++)
{
int t1,t2,t3;
scanf ("%d %d %d",&t1,&t2,&t3);
mapp[t1][t2] = min(mapp[t1][t2] , t3);
}
dijkstra1();
dijkstra2();
int ans = 0;
for (int i = 1 ; i <= n ; i++)
ans = max (ans , f[0][i] + f[1][i]);
printf ("%d\n",ans);
}
return 0;
}
相关文章推荐
- poj 3268 Silver Cow Party(单源最短路径Dijkstra·最小环)
- poj 3268 Silver Cow Party (dijkstra 求最短路)
- POJ 3268 Silver Cow Party(dijkstra最短路)
- POJ ~ 3268 ~ Silver Cow Party (Dijkstra + 逆向建边)
- poj 3268 Silver Cow Party(dijkstra最短路)
- POJ 3268 Silver Cow Party(Dijkstra)
- Silver Cow Party POJ - 3268 (dijkstra)
- POJ - 3268 Silver Cow Party(图论/dijkstra最短路)
- poj 3268 Silver Cow Party(dijkstra ++ 双向最短路)
- POJ 3268 Silver Cow Party (Dijkstra + 优先队列)
- POJ - 3268----Silver Cow Party(Dijkstra)
- Silver Cow Party poj 3268 Dijkstra,spfa,+vector
- POJ 3268 Silver Cow Party(dijkstra)
- poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)
- POJ 3268 Silver Cow Party (最短路dijkstra)
- POJ 3268 - Silver Cow Party(dijkstra)
- POJ 3268 Silver Cow Party (Dijkstra~)
- poj 3268 Silver Cow Party dijkstra基础题!!!入门
- POJ 3268 Silver Cow Party(Dijkstra)
- POJ - 3268 Silver Cow Party(dijkstra)