Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13611   Accepted: 6138

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 ≤ XN). 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: N, M, and X
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

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

题目及算法分析:
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#define N 1000+20
#define INF 0x3f3f3f3f using namespace std; int map[N][N];
int dis[N], ans[N];
bool vis[N];
int n, m, s; int Dijkstra(int s)
{
int i, j, k;
for(i=1; i<=n; i++)
dis[i]=map[s][i];
vis[s]=true;
for(k=1; k<n; k++)
{
int mi=INF, pos;
for(i=1; i<=n; i++)
{
if(vis[i]==false && dis[i]<mi )
{
mi=dis[i]; pos=i;
}
}
vis[pos]=true;
for(j=1; j<=n; j++)
{
if(vis[j]==false && dis[j]>dis[pos]+map[pos][j] )
dis[j]=dis[pos]+map[pos][j];
}
}
for(i=1; i<=n; i++)
{
ans[i]=ans[i]+dis[i];
}
return 0;
} void turn_over()
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<i; j++)
swap(map[i][j], map[j][i] );
}
} int main()
{
scanf("%d %d %d", &n, &m, &s);
int u, v, w; for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(i==j) map[i][j]=0;
else map[i][j]=INF;
} for(int i=0; i<m; i++)
{
scanf("%d %d %d", &u, &v, &w);
map[u][v] = w;
}
memset(vis, false, sizeof(vis));
memset(ans, 0, sizeof(ans));
Dijkstra(s);
turn_over();
memset(vis, false, sizeof(vis));
Dijkstra(s);
int cc=-1;
for(int i=1; i<=n; i++)
{
if(ans[i]>cc && ans[i]<INF )
cc=ans[i];
}
printf("%d\n", cc );
return 0;
}

poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )的更多相关文章

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

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

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

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

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

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

  4. POJ 3268 Silver Cow Party 最短路

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

  5. POJ 3268 Silver Cow Party (双向dijkstra)

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

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

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

  7. poj 3268 Silver Cow Party(最短路)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17017   Accepted: 7767 ...

  8. POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路

    Silver Cow Party One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to ...

  9. POJ 3268 Silver Cow Party 单向最短路

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22864   Accepted: 1044 ...

随机推荐

  1. ntp时间服务同步

    第一种方式:同步到网络时间服务器 # ntpdate time.windows.com将硬件时间设置为当前系统时间. #hwclock –w 加入crontab: 30 8 * * * root /u ...

  2. 转: JDK包含的基本组件

    JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的Java SDK.JDK 是整个Java的 ...

  3. 记一个发HTML格式邮件的问题

    很早做了一个自动发邮件的程序,前一向发现Notes升级后反而CSS样式都没有了. 起初不以为意,反正格式数据在,客户也没说啥,后来觉得这样的态度要不得,小洞不补,大洞吃苦. 于是查查资料,发现浏览器里 ...

  4. linux 系统时间硬件时间同步

    1.设置系统时间:date-s 21/05/2016 date -s 08:21:21 2.系统时间同步到硬件时间: clock --systohc

  5. poj 2479 Maximum sum(递推)

     题意:给定n个数,求两段连续不重叠子段的最大和. 思路非常easy.把原串划为两段.求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n ...

  6. 在非主线程中更新UI

    在非主线程中调用了showMessage方法,结果报错:Can't create handler inside thread that has not called Looper.prepare() ...

  7. python:如何判断字符串中的内容是否都为数字并且把字符串转换为数字

    使用str.isdigit();有两种使用方法 str.isdigit('12345') =====>True str.isdigit('aaaaa')======>False 或者 '1 ...

  8. java Map 实现类的对比

    java为数据结构中的映射定义了一个接口 java.util.Map ,他有四个实现类

  9. matlab中文显示乱码:控制台上的,编辑器的,图片中的

    问题:matlab脚本与函数文件的中文注释显示乱码. 环境:matlab R2016a.Windows 10 home. 解决方案: step1 检查locale值 matlab命令行键入命令 fea ...

  10. jdbc 链接池

    package cn.itcast.jdbc.datasourse; import java.sql.Connection;import java.sql.DriverManager;import j ...