POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)
题目链接:
https://vjudge.net/problem/POJ-3268
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 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<string.h>
#include<algorithm>
using namespace std;
void Dijkstra(int s,int e[][]);
#define inf 99999999
int dis[],book[],e[][],f[][],d[],n,m;
int main()
{
int i,j,k,x,a,b,c,maxn,temp;
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
memset(d,,sizeof(d));
for(i=;i<=n;i++)
for(j=;j<=n;j++)
if(i==j)
{
e[i][j]=;
f[i][j]=;
}
else
{
e[i][j]=inf;
f[i][j]=inf;
} for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(c<e[a][b])
{
e[a][b]=c;
f[b][a]=c;
}
}
Dijkstra(x,e);//回
Dijkstra(x,f);//去
maxn=-;
for(i=;i<=n;i++)
{
if(d[i]>maxn)
maxn=d[i];
}
printf("%d\n",maxn);
}
return ;
}
void Dijkstra(int s,int e[][])
{
int i,j,k,min,u;
for(i=;i<=n;i++)
dis[i]=e[s][i];
memset(book,,sizeof(book));
book[s]=;
for(k=;k<n;k++)
{
min=inf;
for(i=;i<=n;i++)
if(book[i]==&&dis[i]<min)
{
min=dis[i];
u=i;
}
book[u]=;
for(i=;i<=n;i++)
if(book[i]==&&dis[i]>dis[u]+e[u][i])
dis[i]=dis[u]+e[u][i];
}
for(i=;i<=n;i++)
d[i]+=dis[i];
}
POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)的更多相关文章
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13982 Accepted: 6307 ...
- POJ 3268 Silver Cow Party (Dijkstra + 优先队列)
题意:由n个牧场,编号1到n.每个牧场有一头牛.现在在牧场x举办party,每头牛都去参加,然后再回到自己的牧场.牧场之间会有一些单向的路.每头牛都会让自己往返的路程最短.问所有牛当中最长的往返路程是 ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj 3268 Silver Cow Party(最短路dijkstra)
描述: One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the bi ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
随机推荐
- Chapter3_操作符_逻辑操作符
逻辑操作符与(&&)或(||)非(^)能够对布尔类型的数据类型进行操作,并且生成布尔值,和关系操作符的产生的数据类型是一样的.需要注意的不多,有以下几点: (1)在需要使用string ...
- Spring的概念
一.思想 IOC: DI: 二.applicationContext&BeanFactory
- ScriptOJ-flatten2#91
generator的使用 function *flatten2 (arr) { const result = [] function flatten(ar) { ar.map(iter => { ...
- Navicat for MYSQL 断网时本地连接无法打开,2005错误
Navicat for MYSQL 断网时本地连接无法打开,2005错误 NO1 提示下图: NO2 解决方法: (1)选中本地连接,右键 连接属性 (2) 将 主机名或IP地址 这一栏改为 127. ...
- React Native学习方法论
这是我技术公众号的第一篇文章,也是React Native系列文章的第一篇,对我的文章感兴趣的可以加我微信16230091进行关注. 本文表面上讲React Native(以下简称RN),实际上对于学 ...
- poj1149构图题
引题解: 这道题目的大意是这样的:⦁ 有 M 个猪圈(M ≤ 1000),每个猪圈里初始时有若干头猪.⦁ 一开始所有猪圈都是关闭的.⦁ 依次来了 N 个顾客(N ≤ 100),每个顾客分别会打开指定 ...
- Android Studio在项目中添加assets资源目录
第一步: 切换到"Project"视图,找到app --> src --> main目录 第二步: 右键点击main目录,New --> Directory -- ...
- ElasticSearch核心知识总结(一)es的六种搜索方式和数据分析
es的六种搜索方式 query string search GET /ecommerce/product/_search //查询所有数据 { "took": 4,//耗费几毫秒 ...
- Linux - 参考链接
01 - Linux系统监控的Shell脚本 常用的主机监控Shell脚本 使用Shell脚本对Linux系统和进程资源进行监控 02 - Linux系统结构 Linux 系统结构详解 03 - Li ...
- hdu 5116--Everlasting L(计数DP)
题目链接 Problem Description Matt loves letter L. A point set P is (a, b)-L if and only if there exists ...