poj3268俩次SPFA。。。。。
来源:互联网 发布:介绍人工智能的文章 编辑:程序博客网 时间:2024/05/16 14:02
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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10
Hint
俩次SPFA:
题意:
N个农场,每个农场的都有一个奶牛去参加party,M条单向路。
求对于所有奶牛来说花费在路上的最长时间(奶牛都是选择最短的路)
1:以X为源点,各顶点的出边构成邻接表,求源点到各顶点的最短路。
2:以X为源点,各顶点的入边做出边,构成邻接表,求源点到各顶电的最短路。
将两次时间加起来最大的即为答案。
PS(构建邻接表推荐刘汝佳小白上方法)
详细请见代码注释:
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<queue>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x3f // 一个大数#define eps 1e-6using namespace std;#define maxn 1010#define Maxn 100010int n,m,x;int first[maxn];int next[maxn];int u[Maxn];int v[Maxn];int w[Maxn];int dis[maxn];int go[maxn];int vist[maxn];//记录点在不在队列里面int a[Maxn],b[Maxn],c[Maxn];queue<int> Q;void init(){ memset(vist,0,sizeof(vist)); while(!Q.empty()) Q.pop();//一次spfa后记得清空队列 memset(dis,INF,sizeof(dis)); dis[x]=0; for(int i=1;i<=n;i++) first[i]=-1;}void spfa(int x){ Q.push(x); vist[x]=1;//入列记为 1 while(!Q.empty()) { int t=Q.front(); Q.pop(); vist[t]=0;//出列记为 0 for(int i=first[t];i!=-1;i=next[i]) { if(dis[t]+w[i]<dis[v[i]]) { dis[v[i]]=dis[t]+w[i]; if(!vist[v[i]]) { Q.push(v[i]); vist[v[i]]=1; } } } } for(int i=1;i<=n;i++) go[i]+=dis[i];}int main(){ while(~scanf("%d%d%d",&n,&m,&x)) { init(); memset(go,0,sizeof(go)); for(int i=1;i<=m;i++) { scanf("%d%d%d",&a[i],&b[i],&c[i]); u[i]=a[i]; v[i]=b[i]; w[i]=c[i]; next[i]=first[u[i]]; //推荐刘汝佳小白上邻接表的简单写法 first[u[i]]=i; //把后入队的放在队首 } spfa(x);//以X为源点,各顶点的出边构成邻接表,求源点到各顶点的最短路。 init(); for(int i=1;i<=m;i++) { v[i]=a[i]; u[i]=b[i]; w[i]=c[i]; next[i]=first[u[i]]; first[u[i]]=i; } spfa(x); //以X为源点,各顶点的入边构成邻接表,求源点到各顶点的最短路。 int mm=go[1]; for(int i=2;i<=n;i++) if(go[i]>mm) mm=go[i]; printf("%d\n",mm); } return 0;}
- poj3268俩次SPFA。。。。。
- poj3268---spfa
- POJ3268 SPFA
- poj3268 spfa
- poj3268 Silver Cow Party(SPFA)
- POJ3268 Sliver Cow Party SPFA
- 【POJ3268】Silver Cow Party【spfa】
- Silver Cow Party--poj3268(SPFA)
- [spfa/dijkstra]poj3268 Silver Cow Party
- POJ3268 Silver Cow Party(dijkstra,spfa)
- 图论基础SPFA:poj3268模板题
- poj3268
- poj3268
- poj3268
- poj3268
- POJ3268
- poj3268
- poj3268
- hdu 4737A Bit Fun
- easyUI-datagrid 点击触发check事件
- STL迭代器
- EasyUi datagrid选中行的index值
- KVM虚拟机安装
- poj3268俩次SPFA。。。。。
- poj 2886 Who Gets the Most Candies?(线段树+约瑟夫环+反素数)
- iOS 开发之UICollectionView使用详解
- SDL2.0学习笔记4--用SDL画图(SDL_Surface)
- UVA - 12046 Great Numbers
- HDU 1864 最大报销额 背包(处理浮点数小技巧)
- DNS子域授权,主从同步,视图智能DNS,安全特性
- 阻塞IO服务器模型之多线程服务器模型
- Head of a Gang 1446 并查集