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 ...
随机推荐
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测
#include<cstdio> #include<iostream> using namespace std; ][],n,m,fa[],st[]; ]; bool isro ...
- Ubuntu14.04安装和配置ROS Indigo(一)
安装ROS 配置Ubuntu的软件源 配置Ubuntu要求允许接受restricted.universe和multiverse的软件源,可以根据下面的链接配置: https://help.ubuntu ...
- 在其他页面调用 Discuz 7.2 BBS 论坛会员登录信息
require_once './bbs/include/common.inc.php'; $discuz_uid 是用户ID $_DSESSION是用户所有信息
- CoHTMLDocument
http://blog.csdn.net/dlwxn/article/details/2860329 http://www.itnose.net/detail/120267.html 不知道是 线程内 ...
- 【C++】String类实现
//string.h #include <iostream> using namespace std; class String; istream& operator>> ...
- 戴文的Linux内核专题:10配置内核(6)
转自Linux中国 欢迎来到下一篇关于内核配置文章!还有大量的选项需要配置.这篇文章将主要讨论PCI和ACPI. 这里我们可以启用由ACPI控制的扩展坞和可移动驱动器槽的支持(Dock).记住,ACP ...
- NIO基础
通道和缓冲区 概述 通道 和 缓冲区 是 NIO 中的核心对象,几乎在每一个 I/O 操作中都要使用它们. 通道是对原 I/O 包中的流的模拟.到任何目的地(或来自任何地方)的所有数据都必须通过一个 ...
- struts2的返回类型
return 一个字符串,如果是success 直接 服务器端跳转 返回到和方法名对应的页面去 不过如果返回的页面和方法没有太大关系,比如删除修改添加之后要 客户端跳转 返回所有用户列表,这个时候可以 ...
- 新版的tomcat里面get请求通过http协议的时候似乎默认是UTF-8的编码了吗?
不在servler.xml的connector中添加URICoding=“UTF-8”,使用默认值一样没有乱码,而添加URICoding=“iso-8859-1”就是乱码了. POST请求还是用iso ...
- Android布局— — —线性布局
以水平或垂直的方式显示界面中的控件 线性布局 语法格式: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...