概述
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; roadi requiresTi (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
#include<stdio.h>
#include<queue>#include<algorithm>
#include<memory.h>
#include<iostream>
using namespace std;
#define INF 1<<28
#define MAX 1005
int n,m,x,i,j;
int map[MAX][MAX],dto[MAX],dfrom[MAX];
bool visto[MAX],visfrom[MAX];
typedef pair<int ,int>exe;
void dijstrato()
{
memset(visto,0,sizeof(visto));
for(i=1;i<=n;i++)
dto[i]=INF;
dto[x]=0;
priority_queue<exe,vector<exe>,greater<exe> >q;
q.push(make_pair(dto[x],x));
while(!q.empty())
{
exe tmp=q.top();
q.pop();
int now=tmp.second;
if(visto[now])
continue;
visto[now]=true;
for(i=1;i<=n;i++)
if(!visto[i]&&map[now][i]<INF&&dto[i]>dto[now]+map[now][i])
{
dto[i]=dto[now]+map[now][i];
q.push(make_pair(dto[i],i));
}
}
return;
}
void dijstrafrom()
{
memset(visfrom,0,sizeof(visfrom));
for(i=1;i<=n;i++)
dfrom[i]=INF;
dfrom[x]=0;
priority_queue<exe,vector<exe>,greater<exe> >q;
q.push(make_pair(dfrom[x],x));
while(!q.empty())
{
exe tmp=q.top();
q.pop();
int now=tmp.second;
if(visfrom[now])
continue;
visfrom[now]=true;
for(i=1;i<=n;i++)
if(!visfrom[i]&&map[now][i]<INF&&dfrom[i]>map[now][i]+dfrom[now])
{
dfrom[i]=dfrom[now]+map[now][i];
q.push(make_pair(dfrom[i],i));
}
}
return ;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&x))
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=INF;
int x,y,cost;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&cost);
map[x][y]=cost;
}
dijstrato();
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
swap(map[i][j],map[j][i]);
dijstrafrom();
priority_queue<int>p;
for(i=1;i<=n;i++)
p.push(dto[i]+dfrom[i]);
printf("%dn",p.top());
}
return 0;
}
最后
以上就是可靠发带为你收集整理的poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)的全部内容,希望文章能够帮你解决poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复