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

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

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.

Source


Mean:

原意:草场上有n个农场,农场之间有一些路径,每个农场里住着一头牛,现在x农场的牛要过生日开party,其他农场的牛要到该农场去参加party,现在让你选择一头来回耗时最多的一头牛出来,输出时间。

给你一个n个结点、m条边的有向图,现在要你求从n-1个结点到达指定的一个结点的来回最长路。

analyse:

这题思路很巧妙,我们在存图的时候用链式向前星来存,存的时候就建两次边,一次正向,一次反向,用一个flag来标记一下。然后用两遍spfa,第一遍求出从x点出发的正向图到每个结点的最短路,第二遍求出从x点出发的反向图到每个结点的最短路,最后将两次的最短路对应相加,求出最大值即为最终的answer。

Time complexity:O(n*k)

Source code:

//Memory   Time
// 3521K 241MS
//by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAXV 1010
#define MAXE 100010
#define LL long long
using namespace std;
namespace Adj
{
struct Node
{
int to,next,val;
bool flag;
} edge[MAXE<<1];
int top,head[MAXV];
void init()
{
top=1;
memset(head,0,sizeof(head));
}
void addEdge(int u,int v,int val)
{
edge[top].to=v;
edge[top].val=val;
edge[top].flag=1;
edge[top].next=head[u];
head[u]=top++; edge[top].to=u;
edge[top].val=val;
edge[top].flag=0;
edge[top].next=head[v];
head[v]=top++;
}
}
using namespace Adj;
int n,m,x,ans;
bool vis[MAXV];
int dis[MAXV];
int dis1[MAXV];
void spfa(bool flag)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;i++)
dis[i]=INT_MAX;
queue<int>Q;
Q.push(x);
vis[x]=1;
dis[x]=0;
while(!Q.empty())
{
int now=Q.front();
Q.pop();
vis[now]=0;
for(int i=head[now];i;i=edge[i].next)
{
if(flag==1)
{
if(edge[i].flag==0)
continue;
}
else
{
if(edge[i].flag==1)
continue;
}
int son=edge[i].to;
int val=edge[i].val;
if(dis[now]+val<dis[son])
{
dis[son]=dis[now]+val;
if(!vis[son])
{
vis[son]=1;
Q.push(son);
}
}
}
}
if(flag==1)
{
for(int i=1;i<=n;i++)
dis1[i]=dis[i];
}
else
{
int Max=INT_MIN;
for(int i=1;i<=n;i++)
{
dis[i]+=dis1[i];
if(dis[i]>Max)
Max=dis[i];
}
ans=Max;
}
} int main()
{
// freopen("cin.txt","r",stdin);
// freopen("cout.txt","w",stdout);
while(~scanf("%d %d %d",&n,&m,&x))
{
init();
int u,v,w;
while(m--)
{
scanf("%d %d %d",&u,&v,&w);
addEdge(u,v,w);
}
spfa(1);
spfa(0);
printf("%d\n",ans);
}
return 0;
}

  

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party的更多相关文章

  1. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  2. 图论 --- spfa + 链式向前星 (模板题) dlut 1218 : 奇奇与变形金刚

    1218: 奇奇与变形金刚 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 130  Solved: 37[Submit][Status][Web Boa ...

  3. Tarjan模版(链式向前星表示方法)

    这道模版用到了链式向前星表示法: struct node { int v,next; }edge[]; void add(int x,int y) { edge[++cnt].next=heads[x ...

  4. 【数据结构】链式向前星知识点&代码

    代码: struct NODE{ int to; int nxt; int c; }node[MM];//链式向前星 ; void add(int a,int b,int c){ node[lcnt] ...

  5. 【bfs+链式向前星】防御僵尸(defend)计蒜客 - 45288

    题目: A 国有 n 座城市,n−1 条双向道路将这些城市连接了起来,任何两个城市都可以通过道路互通. 某日,A 国爆发了丧尸危机,所有的幸存者现在都聚集到了 A 国的首都(首都是编号为 1 的城市) ...

  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 最短路—dijkstra算法的优化。

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

  8. POJ 3268 Silver Cow Party 最短路

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

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

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

随机推荐

  1. 每周一书-2016年8月15日到21日(bootstrap基础教程)获奖读者公布

    本次赠书 由微信昵称为“………….”的网友以10个赞获得. 请这位网友,订阅号回复你的联系方式,明天给你邮递这本书.谢谢!同时感谢<把时间当朋友>的获奖者“永梅”为<bootsrap ...

  2. ThoughtWorks持续集成平台GO开源了

    ThoughtWorks 持续集成平台Go最近宣布开源了.其基于Apache 2.0 开源协议. Go下载地址为http://www.go.cd/download/. 下面是几张来自官方的视图: GO ...

  3. [.net 面向对象程序设计进阶] (6) Lamda表达式(二) 表达式树快速入门

    [.net 面向对象程序设计进阶] (6) Lamda表达式(二) 表达式树快速入门 本节导读: 认识表达式树(Expression Tree),学习使用Lambda创建表达式树,解析表达式树. 学习 ...

  4. C# 关于Try/Catch对系统性能影响的总结

    自从开始考虑代码的运行效率和性能以后,写代码考虑的东西越来越多了,比如什么时候应该加try/catch?加太多的try/catch会不会降低性能?今天就来分享一下对try/catch对性能影响的一些看 ...

  5. Unity3D热更新全书-PageZero

    由于深刻的认识到自己是个思维跳跃的人,深入浅出是个我还要努力很久的目标,为了让大家不至于在我乱七八糟的文字中迷失,特整理目录一份 无分类 <Unity3D热更新全书-何谓热更新,为何热更新,如何 ...

  6. [异常解决] vmware tools 虚拟机 --> 更新/导入wmwera tools菜单变灰,无法导入问题解决

    在虚拟中的装Ubuntu只要下载一个Ubuntu镜像,按照新建的指示一步一步安装 ,但是安装好之后想从原来系统中复制点东西到虚拟机中的Ubuntu中却有点麻烦.幸好Vmware自带了一个VMware ...

  7. Visual Studio 2015 速递(4)——高级特性之移动开发

    系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...

  8. JAVA环境配置-Eclipse新建项目

    首先配置安装jdk和jre 安装如下: 然后配置变量环境:右键我的电脑--属性--高级系统设置--环境变量--系统变量--找到PATH--编辑 将安装配置的jdk的目录和jdk目录下的bin目录放入其 ...

  9. rabbitmq消息队列——"Hello World!"

    RabbitMQ 一."Hello World!" 1.简介: RabbitMQ是一种消息中间件,主要思想很简单:接收消息并转发.你可以将它设想为一个邮局:你往里面发送邮件并确保邮 ...

  10. 在忘记root密码的情况下如何修改linux系统的root密码

    1.系统启动时长按shift键后可以看到如下界面:     2.找到 recovery mode 那一行, 按下[e]键进入命令编辑状态,到 linux /boot/vmlinuz-....... r ...