题目链接:http://poj.org/problem?id=3268

Silver Cow Party
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

Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

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)的更多相关文章

  1. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:28457   Accepted: 12928 ...

  2. (简单) 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 ...

  3. POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)

    题目链接: https://vjudge.net/problem/POJ-3268 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently n ...

  4. POJ 3268 Silver Cow Party ( Dijkstra )

    题目大意: 有N个农场每个农场要有一头牛去参加一个聚会,连接每个农场有m条路, 聚会地点是X,并且路是单向的.要求的是所有牛赶到聚会地点并且回到自己原先的农场所需要的最短时间. 题目分析: 其实就是以 ...

  5. POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

    POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...

  6. POJ 3268 Silver Cow Party (最短路径)

    POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...

  7. POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  8. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  9. DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards

    题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...

随机推荐

  1. 动态链接库dll键盘钩子后台记录代码示例

    //.header #ifndef _DLLHOOK_H_ #define _DLLHOOK_H_ #include <windows.h> #define DLL_EXPORT_FUN ...

  2. bzoj 2730: [HNOI2012]矿场搭建

    #include<cstdio> #include<cstring> #include<iostream> #define M 508 using namespac ...

  3. daemon

    关于daemon,其最简单的用法是: , ) == -) ; 将上面代码放置程序中,程序执行到这一行,就会自动进入后台运行,不再与终端交互,即终端再输入的参数无效,程序的输出(比如printf等)无效 ...

  4. Rsync+sersync文件实时同步

    一.为什么要用Rsync+sersync架构1.sersync是基于Inotify开发的,类似于Inotify-tools的工具2.sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改 ...

  5. JS编码,解码. asp.net(C#)对应解码,编码

    escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-Z encodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@ ...

  6. Rhel6-lvs配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 相关网址:http://zh.linuxvirtualserver.org/ yum仓库配置: [rh ...

  7. Android 之 JSON操作

    Android默认已经集成了操作JSON相关的API,如下所示: 也可以不使用JSON工具类,直接使用字符串拼接. 注意:可以使用字符串来构造JSONArray和JSONObject,这就是JSON解 ...

  8. obj.offsetHeight与obj.style.height区别

    我们都知道obj.offsetHeight与obj.style.height都可以获取obj的高度,但是在js使用中,我们通常会使用前者来获取高度,这是为什么,二者有什么样的区别呢. 1.obj.of ...

  9. SDK(SoftWare Development Kit)介绍

    ctrl+alt+shift+s进入项目设置页面: SKDs的界面可以设置SDK. 点击到project 可以为project选择sdk 如上图标注 1 所示,IntelliJ IDEA 支持 6 种 ...

  10. iphone判断当前网络连接类型

    eachability只能区分出无网络.wifi和wwan(2G&2.5G&3G)类型的网络连接类型,只需重构networkStatusForFlags方法,即可详细区分出2G与3G网 ...