POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 19211 | Accepted: 8765 |
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 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
题目大意:(单向路)有n头牛,他们商量好去x家举行party,需要你帮他们计算出其他人去到x家party结束后回到自己家所需要的最短路程,然后输出最长路程。(每头牛去的路程可能和回家的路程不同)
解题思路:用两遍dijkstra 分别计算出其他牛到x家的最短路径,然后再计算出他们从x家返回到自己家的最短路程,最后找出最长路径
#include <stdio.h>
#include <string.h>
#define inf 9999999999
int p1[][];
int p2[][];
int vis1[];
int vis2[];
int dis1[];
int dis2[];
int n,m,x;
void dijkstra_go() //计算从自己家到x家所需要的最短路径(注意用的p2数组)反向思维
{
int i,j,pos = ,minn;
for (i = ; i <= n; i ++)
{
vis1[i] = ;
dis1[i] = p2[x][i];
}
vis1[x] = ;
dis1[x] = ; for (i = ; i <= n; i ++)
{
minn = inf;
for (j = ; j <= n; j ++)
{
if (!vis1[j] && dis1[j] < minn)
{
minn = dis1[j];
pos = j;
}
}
vis1[pos] = ;
for (j = ; j <= n; j ++)
{
if (!vis1[j] && dis1[j] > dis1[pos]+p2[pos][j])
dis1[j] = dis1[pos]+p2[pos][j];
}
}
}
void dijkstra_back() //计算从x家回到自己家所需要的最短路径
{
int i,j,pos = ,minn;
for (i = ; i <= n; i ++)
{
vis2[i] = ;
dis2[i] = p1[x][i];
}
vis2[x] = ;
dis2[x] = ;
for (i = ; i <= n; i ++)
{
minn = inf;
for (j = ; j <= n; j ++)
{
if (!vis2[j] && dis2[j] < minn)
{
minn = dis2[j];
pos = j;
}
}
vis2[pos] = ;
for (j = ; j <= n; j ++)
{
if (!vis2[j] && dis2[j] > dis2[pos]+p1[pos][j])
dis2[j] = dis2[pos]+p1[pos][j];
}
}
}
int main ()
{
int a,b,t;
int i,j;
int sum[];
while (~scanf("%d%d%d",&n,&m,&x))
{
for (i = ; i <= n; i ++)
{
for (j = ; j <= n; j ++)
{
p1[i][j] = inf;
p2[i][j] = inf;
}
} for (i = ; i < m; i ++)
{
scanf("%d%d%d",&a,&b,&t);
p1[a][b] = t;
p2[b][a] = t;
}
dijkstra_go();
dijkstra_back();
int maxx = ;
for (i = ; i <= n; i ++)
{
if (i == x)
continue;
sum[i] = dis1[i]+dis2[i];
if (maxx < sum[i])
maxx = sum[i];
}
printf("%d\n",maxx);
}
return ;
}
POJ 3268 Silver Cow Party (双向dijkstra)的更多相关文章
- POJ 3268 Silver Cow Party (Dijkstra)
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions:28457 Accepted: 12928 ...
- (简单) POJ 3268 Silver Cow Party,Dijkstra。
Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to atten ...
- POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)
题目链接: https://vjudge.net/problem/POJ-3268 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently n ...
- POJ 3268 Silver Cow Party ( Dijkstra )
题目大意: 有N个农场每个农场要有一头牛去参加一个聚会,连接每个农场有m条路, 聚会地点是X,并且路是单向的.要求的是所有牛赶到聚会地点并且回到自己原先的农场所需要的最短时间. 题目分析: 其实就是以 ...
- 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 最短路
原题链接: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 ...
随机推荐
- cmd的xcopy命令
C#项目的PostEvent里经常会用到xcopy命令,复制目录时容易出错,如下: xcopy sourceDir targetDir,其中的2个目录最后不能有反斜杠"",而目录类 ...
- Mvc学习--1
1.缓存机制[OutputCache(Duration=10)] 后面的 duration 表示缓存时间 直接放在action上面 是一个特性2.文件上传 @using (Html.BeginForm ...
- SharePoint 2013 开发——APP安全模型
博客地址:http://blog.csdn.net/FoxDave 除非开启了SharePoint网站的匿名访问,否则对于入站的请求,必须要有一个身份验证的过程(Authentication),这个 ...
- Mainstoryboard
页面间进行跳转 [self performSegueWithIdentifier:@"signInSuccess" sender:self] signSuccess是miansto ...
- jQuery 关于 end() 方法的详细解释
<ul class="first"> <li class="foo">list item 1</li> <li> ...
- Non-constant Fields in Case Labels
Non-constant Fields in Case Labels in android library project http://tools.android.com/tips/non-cons ...
- hdu 2048
PS:WA了两次...主要是没注意到fac的大小好像只能写到9...要用long long型递归求阶乘... 然后就是错排公式...百度下.. 代码: #include "stdio.h&q ...
- loadrunner录制时弹出invalid application path!please check if application exists对话框
问题:oadrunner录制时弹出invalid application path!please check if application exists对话框 原因:IE浏览器地址不对,需要手动重新选 ...
- 详解Java中的访问控制修饰符(public, protected, default, private)
Java中的访问控制修饰符已经困惑笔者多时,其中较复杂的情况一直不能理解透彻.今天下定决心,系统.全面地研究Java中的访问控制修饰符的所有方面,并整理成这篇文章,希望有同样疑惑的读者读完后能有所收获 ...
- UIkit框架之Uivew
1.继承链:UIresponder:NSObject 2.通过使用 addGestureRecognizer:方法可以为视图添加手势 3.下面的属性都可以用来用于动画 @property frame ...