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

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

题解:还是正向建图和逆向建图。就是求往返路程中最大的一条。

djistra:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int INF= 0x3fffffff; int map[2][1003][1003];
bool visited[1003];
int d[1003];
int ans[1003]; int max(int a,int b)
{
return a > b ? a : b;
} void prim(int n,int s,int flag)
{
memset(visited,false,sizeof(visited));
for(int i = 1;i <= n;i++)
{
d[i] = map[flag][s][i];
//cout<<d[i]<<endl;
}
visited[s] = true;
for(int i = 1;i < n;i++)
{
int min = INF;
int k;
for(int j = 1;j <= n;j++)
{
if(!visited[j] && min > d[j])
{
min = d[j];
k = j;
}
}
if(min == INF)
{
break;
}
visited[k] = true;
for(int j = 1;j <= n;j++)
{
if(!visited[j] && d[j] > d[k] + map[flag][k][j])
{
d[j] = d[k] + map[flag][k][j];
}
}
}
} int main()
{
int n,m,st;
while(scanf("%d%d%d",&n,&m,&st) != EOF)
{
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(i == j)
{
map[0][i][j] = 0;
map[1][i][j] = 0;
}
else
{
map[0][i][j] = INF;
map[1][i][j] = INF;
}
}
} int u,v,c;
for(int i = 0;i < m;i++)
{
scanf("%d%d%d",&u,&v,&c);
map[0][u][v] = c;
map[1][v][u] = c;
} prim(n,st,1);
for(int i = 1;i <= n;i++)
{
ans[i] = d[i];
}
prim(n,st,0);
int res = 0;
for(int i = 1;i <= n;i++)
{
ans[i] += d[i];
res = max(res,ans[i]);
} printf("%d\n",res);
} return 0;
}

Silver Cow Party的更多相关文章

  1. 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 ...

  2. Silver Cow Party(最短路,好题)

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

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

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

  4. POJ 3268 Silver Cow Party (Dijkstra)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13982   Accepted: 6307 ...

  5. poj 3268 Silver Cow Party

                                                                                                       S ...

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

    Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...

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

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

  8. TOJ1693(Silver Cow Party)

    Silver Cow Party   Time Limit(Common/Java):2000MS/20000MS     Memory Limit:65536KByte Total Submit: ...

  9. POJ3268 Silver Cow Party(dijkstra+矩阵转置)

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15156   Accepted: 6843 ...

  10. POJ 3268 Silver Cow Party (Dijkstra)

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

随机推荐

  1. EntityFramework中几种操作小结

    目前项目中使用到的EntityFramework中几种操作小结,先标记下.没有详细介绍,后续有空的话再补充一些并完善一下. 列中加入RowVersion时间戳 public class Product ...

  2. MemoryModule -- load a DLL completely from memory

    https://github.com/fancycode/MemoryModule MemoryModule is a library that can be used to load a DLL c ...

  3. Druid 配置_StatViewServlet配置

    https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatViewServlet%E9%85%8D%E7%BD%AE Druid内置提供 ...

  4. 杭州有赞公司招聘 资深PHP开发工程师(平台架构方向)、技术专家

  5. MySQL数据库的概念

    学习数据库的一些知识.写写博客方便梳理以及巩固知识. 关于什么是数据库就举一个样例来说明,说的可能不够准确,仅仅要明确一个大概的意思就够了.深刻的学习还是要去看书的. 讲讲生活中有关数据的样例:在一个 ...

  6. eclipse下使用git上传(下载)代码至(从)github

    eclipse下使用git插件上传代码至github 1.eclipse下安装git 正常情况下,eclipse 是自带 git 插件的,那么即可跳至步骤1的最后一小步,配置 git . 如果十分悲剧 ...

  7. whois协议

    1.原理非常简单,域名的查询主要是基于RFC 954提供的WHOIS协议.在上述过程中,我们实际上是访问了InterNIC站点的WHOIS服务器,该服务器从WHOIS数据库中查询我们所需要的内容.WH ...

  8. MySQL数据类型varchar详解

    1.varchar(N)的逻辑意义从MySQL 4.1开始,varchar(N)中的N指的是该字段最多能存储多少个字符(characters),不是字节数.不管是一个中英文字符或者数字.或者一个汉字, ...

  9. django单表操作 增 删 改 查

    一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取出数据. 目的:通过classes(班 ...

  10. powerdesigner 16.5 破解步骤

    假设你的PowerDesigner已经安装完成.(PowerDesigner下载地址:http://pan.baidu.com/s/1mgqjmpa) 1. 从网上下载PowerDesigner165 ...