poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)
来源:互联网 发布:淘宝做单一个月挣多少 编辑:程序博客网 时间:2024/06/05 02:49
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 ofM (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, andTi. The described road runs from farmAi to farmBi, requiringTi 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<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("%d\n",p.top());
}
return 0;
}
- poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)
- poj3268Silver Cow Party——最短路变形
- poj3268Silver Cow Party之dijkstra解法
- POJ3268Silver Cow Party
- POJ3268 Silver Cow Party(最短路问题)
- 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)
- poj 3268 Silver Cow Party (dijkstra 求最短路)
- POJ 3268Silver Cow Party(dijkstra最短路)
- POJ 3268 Silver Cow Party 最短路 dijkstra
- POJ-3268-Silver Cow Party [最短路][Dijkstra]
- 最短路(Dijstra+矩阵转置) Silver Cow Party
- poj3268Silver Cow Party(单源最短路径)
- POJ 3268 Silver Cow Party(最短路 dijkstra求任意两点最短路)
- 部分计算机上视频不能自动刷新的解决方案
- HashTable 使用
- COM创建机制(二)
- Code[VS] 1212 最大公约数
- 快速排序
- poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)
- android 判断手机是否打开了允许模拟位置选项
- UIButton的UIButtonTypeRoundedRect样式导致按钮图象色彩发生改变
- 【LeetCode】【Python】Binary Tree Inorder Traversal
- 同学聚会之后
- .net mvc 微信开发笔记(一)------申请测试用公共账号
- 详解Struts1中的struts-config.xml配置文件
- 喷锡vs沉金
- STL