Silver Cow Party

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: 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.
 
 
题意:cow很懒。。他们要参加一个party,想知道从最短路往返走哪头cow用时最多,所有的cow都会卡着点到达并返回。。已知一点,求其他点到这点再返回各点沿最短路走的最大用时。
思路:这次练了练SPFA+SLF(双向队列优化)。之前做的单源最短路是从一个点出发,到各点的最短路,这道题加上了从所有点出发到一个点的最短路,只需将有向图倒着存储,正着找就可以了。
 
#include<stdio.h>
#include<string.h>
#include<deque>
#include<vector>
#define MAX 1005
#define INF 0x3f3f3f3f
using namespace std; struct Node{
int v,w;
}node;
vector<Node> edge[MAX],redge[MAX];
int dis[MAX],diss[MAX],b[MAX];
int n;
void spfa(int k)
{
int i;
deque<int> q; //双向队列
for(i=;i<=n;i++){
dis[i]=INF;
diss[i]=INF;
}
memset(b,,sizeof(b));
b[k]=;
dis[k]=;
q.push_back(k);
while(q.size()){
int u=q.front();
for(i=;i<edge[u].size();i++){
int v=edge[u][i].v;
int w=edge[u][i].w;
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
if(b[v]==){
b[v]=;
if(dis[v]>dis[u]) q.push_back(v); //SLF
else q.push_front(v);
}
}
}
b[u]=;
q.pop_front();
}
b[k]=;
diss[k]=;
q.push_back(k);
while(q.size()){
int u=q.front();
for(i=;i<redge[u].size();i++){
int v=redge[u][i].v;
int w=redge[u][i].w;
if(diss[v]>diss[u]+w){
diss[v]=diss[u]+w;
if(b[v]==){
b[v]=;
if(diss[v]>diss[u]) q.push_back(v); //SLF
else q.push_front(v);
}
}
}
b[u]=;
q.pop_front();
}
}
int main()
{
int m,x,u,v,w,i;
scanf("%d%d%d",&n,&m,&x);
for(i=;i<=n;i++){
edge[i].clear();
redge[i].clear();
}
for(i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
node.v=v;
node.w=w;
edge[u].push_back(node);
node.v=u;
redge[v].push_back(node);
}
spfa(x);
int max=;
for(i=;i<=n;i++){
if(dis[i]+diss[i]>max&&dis[i]!=INF&&diss[i]!=INF) max=dis[i]+diss[i];
}
printf("%d\n",max);
return ;
}

POJ - 3268 Silver Cow Party SPFA+SLF优化 单源起点终点最短路的更多相关文章

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

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

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

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

  3. POJ 3268 Silver Cow Party 最短路

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

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

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

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

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

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

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

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

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

  8. POJ 3268 Silver Cow Party (Dijkstra)

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

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

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

随机推荐

  1. exception_action

    for i in range(3, -2, -1): try: print(4 / i) except Exception as e: print(Exception) print(e)

  2. Javascript模块化编程-初识[1]

    JS模块化编程,已经成为一个迫切的需求.理想情况下,开发者只需要实现核心业务逻辑,其他都可以加载别人已经写好的模块. 但是,JS不是一种模块化编程语言,它不支持类,所以没有严格意义上的模块.为了实现模 ...

  3. Hadoop实战-Flume之自定义Sink(十九)

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  4. Mac下php版本不支持imagetfftext函数问题

    brew rm freetype jpeg libpng gd zlib brew install freetype jpeg libpng gd zlib brew install php71 ht ...

  5. ESP8266串口模块的基本使用【转】

    本文转载自:http://www.shaoguoji.cn/2017/01/15/ESP8266-usage/ ESP8266是一款超低功耗的UART-WiFi 透传模块,拥有业内极富竞争力的封装尺寸 ...

  6. hadoop —— teragen & terasort

    这两个类所在目录: hadoop-examples-0.20.2-cdh3u6.jar 中: 代码: TeraGen.java: /** * Licensed to the Apache Softwa ...

  7. HDU4825 Xor Sum —— Trie树

    题目链接:https://vjudge.net/problem/HDU-4825 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  8. ActivemMQ之消息服务器平台(发邮件)

    消息服务平台 处理公司内部各种消息业务 比如 发送邮件  发送短信  微信推送 接口有两种类型 异步 同步 同步需求: 当调用消息服务平台,需要返回消息服务平台调用第三方平台接口是否成功 异步需求: ...

  9. windows下的git使用指令

    经常使用mac和linux 这次使用window开发了一些小项目 废话不多说: git init git add . git commit -m 'note' git remote add origi ...

  10. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...