POJ-3268
| 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 ≤ 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
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
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
/**
题意:最短路中哪个走的路程最大
解法: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的更多相关文章
- DIjkstra(反向边) POJ 3268 Silver Cow Party || POJ 1511 Invitation Cards
题目传送门 1 2 题意:有向图,所有点先走到x点,在从x点返回,问其中最大的某点最短路程 分析:对图正反都跑一次最短路,开两个数组记录x到其余点的距离,这样就能求出来的最短路以及回去的最短路. PO ...
- POJ 3268 Silver Cow Party (最短路径)
POJ 3268 Silver Cow Party (最短路径) Description One cow from each of N farms (1 ≤ N ≤ 1000) convenientl ...
- POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】
Silver Cow Party Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- poj 3268(spfa)
http://poj.org/problem?id=3268 对于这道题,我想说的就是日了狗了,什么鬼,定义的一个数值的前后顺序不同,一个就TLE,一个就A,还16MS. 感觉人生观都奔溃了,果然,题 ...
- POJ 3268 Silver Cow Party (双向dijkstra)
题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj - 3268 Silver Cow Party (求给定两点之间的最短路)
http://poj.org/problem?id=3268 每头牛都要去标号为X的农场参加一个party,农场总共有N个(标号为1-n),总共有M单向路联通,每头牛参加完party之后需要返回自己的 ...
- <poj - 3268> Silver Cow Party 牛のpart 最短路径问题
本题链接 : http://poj.org/problem?id=3268 题目大意:牛们要去聚会,输入N = 顶点数(牛场):M = 边(路)的数目: X = 终点 (聚会点).问题:求来回时间的最 ...
- POJ 3268 (dijkstra变形)
题目链接 :http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveni ...
- POJ 3268 Silver Cow Party 最短路
原题链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total ...
随机推荐
- javascript forEach无法break,使用every代替
every的入口参数是一个返回bool值的函数,在需要break的地方return false,其他均return true,即可达到和break相同的效果 function find(arr2, e ...
- bzoj4300: 绝世好题(DP)
按位DP f[i]表示第i位为1的最长子序列 #include<iostream> #include<cstring> #include<cstdlib> #inc ...
- BZOJ1832 聚会
Description:Y岛风景美丽宜人,气候温和,物产丰富.Y岛上有N个城市,有N-1条城市间的道路连接着它们.每一条道路都连接某两个城市.幸运的是,小可可通过这些道路可以走遍Y岛的所有城市.神奇的 ...
- 【并查集】【P1525】关押罪犯
传送门 Description Input Output Sample Input Sample Output Hint Solution 非常显然的并查集题目,在本题中,对于每个罪犯i,维护两个信息 ...
- Widows与linux关于隐形文件和非隐形文件の对比
Widows与linux关于隐形文件和非隐形文件の对比 对于windows来说 ,它本身有一些隐藏文件,为了防止一些菜鸟不小心把电脑的主要文件删除,还有就是里面存放一些你不知道的后门. 对此我们一些同 ...
- flush priviliege
grant all on *.* to usernmae@'%' identified by '*****'; flush privileges grant all on *.* to root@'% ...
- http中有关缓存相关的几个字段
转载自:http://blog.csdn.net/lifeibo/article/details/5979572 Expires.Cache-Control.Last-Modified. ETag是R ...
- js的作用域深入理解
一.什么是作用域 作用域是指对某一变量和方法具有访问权限的代码空间,Javascript的作用域只有两种:全局作用域和本地作用域,本地作用域是按照函数来区分的(即全局变量和局部变量)) 局部变量:只有 ...
- 【算法日记】2.算法中的大O符号
大O符号是一种算法复杂度的相对表示方式. 1.大O表示算法的操作数,表示出算法运行的快慢 2.大O表示法指出了最糟糕情况下的运行时间,例如 简单查找的运行时间O(n),意味着在最糟糕的情况下,必须运行 ...
- 【BZOJ4514】【SDOI2016】数字配对 [费用流]
数字配对 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 有 n 种数字,第 i 种数字是 ...