poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
来源:互联网 发布:java 对象的大小 编辑:程序博客网 时间:2024/06/05 07:40
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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10
Hint
#include <cstring>
using namespace std;
int const enf=100000000;
int p[1001][1001],d[1009],s[1000],d1[1009],d2[1009],n,m;
void r_djsk(int t)
{
memset(d1,0,sizeof(d1));
//集合s初始化,d[]初始化
for(int i=0;i<=n;i++)
{
s[i]=false;
d1[i]=p[i][t];
}
s[t]=true;d1[t]=0;
for(int i=1;i<=n;++i)
{
int k=0;
int cot=1000000;
//在剩余点中找到一个距离他最短的点
for(int j=1;j<=n;++j)
if(!s[j]&&d1[j]<cot)
{
k=j;
cot=d1[j];
}
s[k]=true;//把这个点加入这个集合
//对所有点在更新一遍
for(int j=1;j<=n;++j)
{
if(!s[j]&&(p[j][k]+d1[k])<d1[j])
d1[j]=p[j][k]+d1[k];
}
}
}
void djsk(int t)
{
memset(d,0,sizeof(d));
//集合s初始化,d[]初始化
for(int i=1;i<=n;i++)
{
s[i]=false;
d[i]=p[t][i];
}
s[t]=true;d[t]=0;
for(int i=1;i<=n;++i)
{
int k=0;
int cot=1000000;
//在剩余点中找到一个距离他最短的点
for(int j=1;j<=n;++j)
if(!s[j]&&d[j]<cot)
{
k=j;
cot=d[j];
}
s[k]=true;//把这个点加入这个集合
//对所有点在更新一遍
for(int j=1;j<=n;++j)
{
if(!s[j]&&(p[k][j]+d[k])<d[j])
d[j]=p[k][j]+d[k];
}
}
}
int main()
{
int x;
while(cin>>n>>m>>x)
{
for(int i=1;i<=1000;++i)
for(int j=1;j<=1000;++j)
{
if(i==j)
p[i][j]=0;
else
p[i][j]=enf;
}
for(int i=1;i<=m;++i)
{
int a,b,c;
cin>>a>>b>>c;
p[a][b]=c; //有向图时 p[b][a]=c;
}
djsk(x);
r_djsk(x);
//for(int i=1;i<=n;i++) cout<<d1[i]<<endl;
int ma=-1;
for(int i=1;i<=n;i++)
{
// cout<<d[i]<<" "<<d1[i]<<endl;
d2[i]=d[i]+d1[i];
if(ma<d2[i]) ma=d2[i];
}
cout<<ma<<endl;
}
return 0;
}
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
- poj 3268 Silver Cow Party 【最短路,有向图】
- POJ-3268 Silver Cow Party (Dijkstra求有向图最短路)
- poj 3268 Silver Cow Party(最短路)
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- poj 3268 Silver Cow Party(最短路)
- POJ-3268 Silver Cow Party(最短路spfa算法)
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
- poj-3268 Silver Cow Party (最短路-spfa-求到同一起点,同一终点的最短路)
- poj 3268 Silver Cow Party(最短路+SPFA)
- poj 3268 Silver Cow Party 最短路/dijkstra
- poj 3268 Silver Cow Party(dijkstra最短路)
- POJ 3268 Silver Cow Party 最短路 dij
- POJ 3268 - Silver Cow Party(最短路dijkstra)
- Session实现原理
- Easy-33
- myeclipse修改Web项目根目录下的名称
- iOS并发编程(四)-Dispatch Sources-(待改阅……)
- Unity环境搭建
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
- IOS crash处理
- 已安装php 编译安装 gd库拓展模块
- 安卓接入支付宝出现“创建交易异常,请重新创建后再付款
- AR系列高通芯片通过调用ubus来处理连接客户端的问题
- 使用Docker
- 第一篇博客
- C++与Java比较
- log(Log.v,Log.d,Log.i,Log.w,Log.e)的用法