Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13738   Accepted: 6195

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

/**
题意:最短路中哪个走的路程最大
解法:SPFA
**/
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define maxn 1000 + 10
#define INF 0x3f3f3f3f
using namespace std;
int vis[maxn];
int dist[maxn];
int dist1[maxn];
int flag[maxn];
struct Node
{
int v;
int cost;
Node(int _v,int _cost):v(_v),cost(_cost) {}
};
vector<Node>edge[maxn];
vector<Node>edge1[maxn];
int n,m,p;
void addedge(int u,int v,int w)
{
edge[u].push_back(Node(v,w));
edge1[v].push_back(Node(u,w));
}
void SPFA(int start)
{
memset(vis,,sizeof(vis));
memset(dist,INF,sizeof(dist));
queue<int>que;
que.push(start);
vis[start] = ;
dist[start] = ;
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge[tt].size(); i++)
{
int mm = edge[tt][i].v;
if(dist[mm] > dist[tt] + edge[tt][i].cost)
{
dist[mm] = dist[tt] + edge[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
memset(vis,,sizeof(vis));
memset(dist1,INF,sizeof(dist1));
while(!que.empty()) que.pop();
vis[start] = ;
dist1[start] = ;
que.push(start);
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge1[tt].size(); i++)
{
int mm = edge1[tt][i].v;
if(dist1[mm] > dist1[tt] + edge1[tt][i].cost)
{
dist1[mm] = dist1[tt] + edge1[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
return ;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
scanf("%d %d %d",&n,&m,&p);
int u,v,w;
for(int i=; i<m; i++)
{
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
}
SPFA(p);
int mmax = -INF;
for(int i=; i<=n; i++)
{
if(dist[i] + dist1[i] > mmax && dist[i] != INF && dist1[i]!= INF )
mmax = dist[i] + dist1[i];
}
printf("%d\n",mmax);
return ;
}
/**
题意:最短路中哪个走的路程最大
解法:SPFA
**/
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define maxn 1000 + 10
#define INF 0x3f3f3f3f
using namespace std;
int vis[maxn];
int dist[maxn];
int dist1[maxn];
int flag[maxn];
struct Node
{
int v;
int cost;
Node(int _v,int _cost):v(_v),cost(_cost) {}
};
vector<Node>edge[maxn];
vector<Node>edge1[maxn];
int n,m,p;
void addedge(int u,int v,int w)
{
edge[u].push_back(Node(v,w));
edge1[v].push_back(Node(u,w));
}
void SPFA(int start)
{
memset(vis,,sizeof(vis));
memset(dist,INF,sizeof(dist));
queue<int>que;
que.push(start);
vis[start] = ;
dist[start] = ;
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge[tt].size(); i++)
{
int mm = edge[tt][i].v;
if(dist[mm] > dist[tt] + edge[tt][i].cost)
{
dist[mm] = dist[tt] + edge[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
memset(vis,,sizeof(vis));
memset(dist1,INF,sizeof(dist1));
while(!que.empty()) que.pop();
vis[start] = ;
dist1[start] = ;
que.push(start);
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge1[tt].size(); i++)
{
int mm = edge1[tt][i].v;
if(dist1[mm] > dist1[tt] + edge1[tt][i].cost)
{
dist1[mm] = dist1[tt] + edge1[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
return ;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
scanf("%d %d %d",&n,&m,&p);
int u,v,w;
for(int i=; i<m; i++)
{
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
}
SPFA(p);
int mmax = -INF;
for(int i=; i<=n; i++)
{
if(dist[i] + dist1[i] > mmax && dist[i] != INF && dist1[i]!= INF )
mmax = dist[i] + dist1[i];
}
printf("%d\n",mmax);
return ;
}
/**
题意:最短路中哪个走的路程最大
解法:SPFA
**/
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#define maxn 1000 + 10
#define INF 0x3f3f3f3f
using namespace std;
int vis[maxn];
int dist[maxn];
int dist1[maxn];
int flag[maxn];
struct Node
{
int v;
int cost;
Node(int _v,int _cost):v(_v),cost(_cost) {}
};
vector<Node>edge[maxn];
vector<Node>edge1[maxn];
int n,m,p;
void addedge(int u,int v,int w)
{
edge[u].push_back(Node(v,w));
edge1[v].push_back(Node(u,w));
}
void SPFA(int start)
{
memset(vis,,sizeof(vis));
memset(dist,INF,sizeof(dist));
queue<int>que;
que.push(start);
vis[start] = ;
dist[start] = ;
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge[tt].size(); i++)
{
int mm = edge[tt][i].v;
if(dist[mm] > dist[tt] + edge[tt][i].cost)
{
dist[mm] = dist[tt] + edge[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
memset(vis,,sizeof(vis));
memset(dist1,INF,sizeof(dist1));
while(!que.empty()) que.pop();
vis[start] = ;
dist1[start] = ;
que.push(start);
while(!que.empty())
{
int tt = que.front();
que.pop();
vis[tt] = ;
for(int i=; i<edge1[tt].size(); i++)
{
int mm = edge1[tt][i].v;
if(dist1[mm] > dist1[tt] + edge1[tt][i].cost)
{
dist1[mm] = dist1[tt] + edge1[tt][i].cost;
if(!vis[mm])
{
vis[mm] = ;
que.push(mm);
}
}
}
}
return ;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
scanf("%d %d %d",&n,&m,&p);
int u,v,w;
for(int i=; i<m; i++)
{
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
}
SPFA(p);
int mmax = -INF;
for(int i=; i<=n; i++)
{
if(dist[i] + dist1[i] > mmax && dist[i] != INF && dist1[i]!= INF )
mmax = dist[i] + dist1[i];
}
printf("%d\n",mmax);
return ;
}

POJ-3268的更多相关文章

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

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

  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——————【最短路、Dijkstra、反向建图】

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

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

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

  5. poj 3268(spfa)

    http://poj.org/problem?id=3268 对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS. 感觉人生观都奔溃了,果然,题 ...

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

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

  7. poj - 3268 Silver Cow Party (求给定两点之间的最短路)

    http://poj.org/problem?id=3268 每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的 ...

  8. <poj - 3268> Silver Cow Party 牛のpart 最短路径问题

    本题链接 : http://poj.org/problem?id=3268 题目大意:牛们要去聚会,输入N = 顶点数(牛场):M = 边(路)的数目: X = 终点 (聚会点).问题:求来回时间的最 ...

  9. POJ 3268 (dijkstra变形)

    题目链接 :http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveni ...

  10. POJ 3268 Silver Cow Party 最短路

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

随机推荐

  1. BZOJ3922 Karin的弹幕 【线段树】

    题目链接 BZOJ3922 题解 考虑暴力,修改\(O(1)\),查询\(O(\frac{n}{d})\) 考虑线段树,如果对每种差值建一棵线段树,修改\(O(nlogn)\),查询\(O(logn) ...

  2. 20165218 2017-2018-1《Java程序设计》第二周学习总结

    20165218 2017-2018-1 <Java程序设计>第2周学习总结 教材学习内容总结 Ch2 基本数据类型与数组 Unicode字符集之中所有都叫做"字母", ...

  3. React中的高阶组件,无状态组件,PureComponent

    1. 高阶组件 React中的高阶组件是一个函数,不是一个组件. 函数的入参有一个React组件和一些参数,返回值是一个包装后的React组件.相当于将输入的React组件进行了一些增强.React的 ...

  4. bzoj2724: [Violet 6]蒲公英(离散化+分块)

    我好弱啊..这题调了2天QwQ 题目大意:给定一个长度为n(n<=40000)的序列,m(m<=50000)次询问l~r之间出现次数最多的数.(区间众数) 这题如果用主席树就可以不用处理一 ...

  5. 【状压DP】【UVA11795】 Mega Man's Mission

    传送门 Description 你要杀n个怪,每杀掉一个怪那个怪会掉落一种武器,这种武器可以杀死特定的怪.游戏初始你有一把武器,能杀死一些怪物.每次只能杀一只,求有多少种杀怪方法. Input 多组数 ...

  6. 重载(Overload)和重写(Override)的区别。重载的方法能否根据返回类型进行区分?

    方法的重载和重写都是实现多态的方式,区别在于前者实现的是编译时的多态性,而后者实现的是运行时的多态性.重载发生在一个类中,同名的方法如果有不同的参数列表(参数类型不同.参数个数不同或者二者都不同)则视 ...

  7. 一图看懂深度学习框架对比----Caffe Torch Theano TensorFlow

      Caffe Torch Theano TensorFlow Language C++, Python Lua Python Python Pretrained Yes ++ Yes ++ Yes ...

  8. Asp.net Web Api 2 FORM Authentication Demo

    最近看了一点 web api 2方面的书,对认证都是简单介绍了下,所以我在这里做个简单Demo,本文主要是FORM Authentication,顺带把基本认证也讲了. Demo 一.FORM Aut ...

  9. bzoj1862/1056: [Zjoi2006]GameZ游戏排名系统

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.ph ...

  10. MongoDB 数据库(1)

    数据库 MongoDB (芒果数据库) 数据存储阶段 文件管理阶段 (.txt .doc .xls) 优点 : 数据可以长期保存 可以存储大量的数据 使用简单 缺点 : 数据一致性差 数据查找修改不方 ...