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 ...
随机推荐
- 3.5缺少动态连接库.so--cannot open shared object file: No such file or directory
总结下来主要有3种方法:1. 用ln将需要的so文件链接到/usr/lib或者/lib这两个默认的目录下边 ln -s /where/you/install/lib/*.so /usr/lib sud ...
- limit 百万级数据分页优化方法
mysql教程 这个数据库教程绝对是适合dba级的高手去玩的,一般做一点1万 篇新闻的小型系统怎么写都可以,用xx框架可以实现快速开发.可是数据量到了10万,百万至千万,他的性能还能那么高吗? 一点小 ...
- java调用c++生成的动态和静态库时遇到的问题
java.lang.UnsatisfiedLinkError: no jacob in java.library.path -Djava.library.path 关于java用jni调用 dll动态 ...
- [开发笔记]-VS2012打开解决方案崩溃或点击项目崩溃
下午在使用VS2012建立Service服务项目时,只要一切换到设计视图页面中,VS就崩溃重启,从网上找了一种方法来解决,测试可行.但导致该问题的原因未知. 解决方案: 步骤1:开始-->所有程 ...
- 神奇的NOIP模拟赛 T2 LGTB 学分块
LGTB 学分块 LGTB 最近在学分块,但是他太菜了,分的块数量太多他就混乱了,所以只能分成3 块今天他得到了一个数组,他突然也想把它分块,他想知道,把这个数组分成3 块,块可以为空.假设3 块各自 ...
- IE9的css hack
以前写过<IE8的css hack>,ie9一出css hack也该更新,以前一直没关注,今天在内部参考群mxclion分享了IE9的css hack,拿出来也分享一下: select { ...
- Rhel6-集群管理(luci&&ricci)配置文档
理论基础: User → HA → Lb → web → sql → 分布式filesystem ->磁盘I/O 用户 高可用 负载均衡 应用 数据库 mf ...
- mysql 启动错误1026
进入“事件查看器”“应用程序”果然发现很多MySql的错误Default storage engine (InnoDB) is not available 于是进入MySql的安装目录找到my.ini ...
- matlab 画框(一)
matlab进行图像处理之后,很多时候需要在图像上画出矩形框:如,调用matlab的某个检测函数,得到结果之后,往往需要将检测结果的矩形框画在图像上,直观.方便的进行查看:下面的代码就是这个目的: f ...
- navtab方法参数以及事件
参数(options) DOM方式初始化navtab的,推荐使用集合属性data-options定义参数,如果使用data属性定义参数,注意转换成对应的名称. 名称 类型 默认值 描述 id stri ...