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 ...
随机推荐
- xpath获取一个标签下的多个同级标签
一.问题: 我在使用xpath获取文章内容的时候会遇到,多个相同的标签在同一级下面,但是我们只需要获取一部分的内容.比如我不想需要原标题这些内容. 二.解决: Xpath中有一个position()的 ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;问题的解决
哇,时隔两天时间,终于找到这个问题的解决办法,先看问题 这是我最近写的家庭记账本网页版,按顺序输入点击保存,总是弹出添加失败的提示 顺着找原因,把原因锁定在dao层的sql语句上,反复检查,没有找到一 ...
- overlay fs挂载及操作测试
overlayfs是目前使用比较广泛的层次文件系统,实现简单,性能较好,可以充分利用不同或则相同overlay文件系统的page cache,具有 上下合并 同名遮盖 写时拷贝 等特点. 一个 ove ...
- 简单好用的时间选择插件My97datepicker
我们经常会需要验证字符串的格式,比如密码长度范围.电子邮件格式.固定电话号码和手机号码格式等,这个时候我们经常会需要用到正则表达式.但是正则表达式用起来性能会低一点,所以在需要验证的时候能不使用正则表 ...
- Html5与Css3知识点拾遗(六)
web字号 px() 百分比(较常用) em(最常用):一般1em=16px,相对单位.以父元素作参照系,父元素大小是不确定的 body{ font-size:100%; /*父元素默认为16px*/ ...
- oracle数据库创建分区表
参考资料:http://blog.chinaunix.net/uid-21943216-id-4062400.html 一.建按月自增分区表:1.1建表SQL> create table mon ...
- Altera 在线资源使用
Altera 在线资源使用 Altera 在线资源使用 1 1.Altera中文版 2 2.建立myaltera账户 获取官网信息与支持 2 3系统化的设计资源 2 3.1.设计实例 2 3.2.参考 ...
- ajax 删除数据无刷新
//html页面 <!doctype html><head> <title></title> <meta http-equiv="Con ...
- GCD on Blackboard
题目大意:给你n个数,然后在这n个数中选一个数,选中的这个数可以变成任意的数,使这n个数的gcd(最大公约数)最大.打印这个最大的gcd. 思路:这题一看貌似很复杂,其实这题只要你知道前缀和 和 ...
- jdk8新特性(详解)
最近在复习外加看点面试题,jdk8的新特性虽然在项目用用到过一两个,准备系统的了解一下jdk8的常用新特性 一:Lambd表达式 也可称为闭包 引入函数式编程到Java中 为了使现有函 ...