本文主要是介绍Silver Cow Party(USACO 07 FEB POJ3268),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Silver Cow Party
(USACO 07 FEB)
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 21673
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
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.
Source
USACO 2007 February Silver
Translation
翻译
N头牛都要去参加一场在编号为X(1≤X≤N)的牛的农场举行的派对(1≤N≤1000),农场之间有M(1≤M≤100000)条有向路,每条路长Ti(1≤Ti≤100)。
每头牛参加完派对后都必须回家,无论是去参加派对还是回家,每头牛都会选择最短路径,求这N头牛的最短路径(一个来回)中最长的一条路径长度。
输入输出格式
输入格式:
第一行三个整数N,M, X;
第二行到第M+1行:每行有三个整数Ai,Bi, Ti ,表示有一条从Ai农场到Bi农场的道路,长度为Ti。
输出格式:
一个整数,表示最长的最短路得长度。
输入输出样例
输入样例#1:
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
输出样例#1:
10
今天我们接着说SPFA,借助这道题加深巩固一下。
说起最短路这样的图论题,还是要多做的。那么我们今天就看看USACO的07年2月题目–Silver Cow Party(银牛聚会)。同时也是POJ3268的题目。
这个题与请柬(双向SPFA及SLF LLL优化法模板题)很相似,可以先了解一下这道题,会对本题算法有帮助↑↑↑。
然后就说说思路,本题其实就是给你一个带权值的有向图,找出最小的路径,能让cow从start点回家,再回到start点。很容易想到双向最短路,这里我们仍然是用SPFA,准备两组链表,一个记录正方向,一个记录反方向,然后先正向刷一遍SPFA,再逆向刷一遍,然后把两次的加和都拿出来比较比较,求一个MAX,输出即可。
这里我们仍然用了SLF与LLL,其实这个题朴素SPFA应该也能过。
结果
秀一下结果吧。
POJ
Problem: 3268 User: Stockholm
Memory: 416K Time: 63MS
Language: C++ Result: Accepted
洛谷
代码
(C++)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<deque>
#include<cstring>
using namespace std;
int i,j,m,n,ii,s;
int sum,tot,len[1006],len2[1006];
bool b[1005];
deque<int> q;
struct data
{int y,v;struct data *nxt;
}a[100001],e[100001];int head[1005],hed2[1005],toto;
int r()
{char ch=getchar();int ans=0,f=1;while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch<='9'&&ch>='0'){ans*=10;ans+=ch-'0';ch=getchar();}return ans;
}
int spfa(int x)
{tot=1;sum=0;memset(len,0x7f7f7f,sizeof(len));len[x]=0;q.push_front(x);b[x]=1;struct data *p;while(!q.empty()){p=&a[head[q.front()]];x=q.front();q.pop_front();b[x]=0;tot--;sum-=len[p->y];while(p->y){int yy=p->y;if(len[yy]>p->v+len[x]){len[yy]=p->v+len[x];if(!b[yy]){b[yy]=1;if(q.empty()||len[yy]*tot<=sum||len[yy]>len[q.front()])q.push_back(yy);elseq.push_front(yy);tot++;sum+=len[yy];}}p=p->nxt;}}
}
void spfa2(int x)
{tot=1;sum=0;memset(len2,0x7f7f7f,sizeof(len2));len2[x]=0;q.push_front(x);b[x]=1;struct data *p;while(!q.empty()){p=&e[hed2[q.front()]];x=q.front();q.pop_front();b[x]=0;tot--;sum-=len2[p->y];while(p->y){int yy=p->y;if(len2[yy]>p->v+len2[x]){len2[yy]=p->v+len2[x];if(!b[yy]){b[yy]=1;if(q.empty()||len2[yy]*tot<=sum||len2[yy]>len2[q.front()])q.push_back(yy);elseq.push_front(yy);tot++;sum+=len2[yy];}}p=p->nxt;}}
}
int main()
{n=r();m=r();s=r();int xx;for(i=1;i<=m;i++){xx=r();a[i].y=r();a[i].v=r();a[i].nxt=&a[head[xx]];head[xx]=i;e[i].v=a[i].v;e[i].y=xx;e[i].nxt=&e[hed2[a[i].y]];hed2[a[i].y]=i;}spfa(s);memset(b,0,sizeof(b));spfa2(s);xx=0;for(i=1;i<=n;i++)xx=max(xx,len[i]+len2[i]);cout<<xx;
}
这篇关于Silver Cow Party(USACO 07 FEB POJ3268)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!