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. 洛谷4578 & LOJ2520:[FJOI2018]所罗门王的宝藏——题解

    https://www.luogu.org/problemnew/show/P4578 https://loj.ac/problem/2520 有点水的. 先转换成图论模型,即每个绿宝石,横坐标向纵坐 ...

  2. 洛谷 P2446 [SDOI2010]大陆争霸 解题报告

    P2446 [SDOI2010]大陆争霸 题目背景 在一个遥远的世界里有两个国家:位于大陆西端的杰森国和位于大陆东端的克里斯国.两个国家的人民分别信仰两个对立的神:杰森国信仰象征黑暗和毁灭的神曾·布拉 ...

  3. HDOJ.1075 What Are You Talking About(map)

    What Are You Talking About 点我跳转到题面 点我一起学习STL-MAP 题意分析 首先第一组START-END给出翻译的字典,第二组START-END给出一句话,查找里面出现 ...

  4. PHP 无限级分类树

    1. function generateTree($items){    $tree = array();    foreach($items as $item){        if(isset($ ...

  5. 背景建模技术(五):视频捕获(VideoCapture)模块

    本次对“视频捕获(VideoCapture)模块”做出分析,给出源代码和对应的程序流程框架. 视频捕获模块的主要功能是设置视频或相机参数,并读取设置配置参数,最后进入帧处理模块的process进程,该 ...

  6. [freemarker篇]03.如何处理空值

    我想说的一点,我写的东西没有那么权威,这都是我实际开发中使用的,可能缺少很多! 例如这篇要说的如何处理空值,我发现我使用的跟网上很多写的不太一样,我也没有过多的去尝试网上的那么多写法! 抱歉,我只是写 ...

  7. 【转】Pyhton 单行、多行注释符号使用方法及规范

    转自:Pyhton 单行.多行注释符号使用方法及规范 python中的注释有多种,有单行注释,多行注释,批量注释,中文注释也是常用的.python注释也有自己的规范,在文章中会介绍到.注释可以起到一个 ...

  8. MyISAM和InnoDB的行格式ROW_FORMAT

    MyISAM行存储 MyISAM有3种行存储格式:fixed / dynamic / compressed: 格式 说明 备注   fixed  只有当表不包含变长字段(varchar/varbina ...

  9. ZOJ3874 Permutation Graph

    Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward has a permutation {a1, a2, … an}. He finds ...

  10. bzoj 1084 DP

    首先对于m==1的情况非常容易处理(其实这儿因为边界我错了好久...),直接DP就好了,设f[i][k]为这个矩阵前i个选k个矩阵的最大和,那么f[i][k]=max(f[j][k-1]+sum[j+ ...