bzoj 1631: [Usaco2007 Feb]Cow Party 解题报告
2017-08-25 14:32
483 查看
1631: [Usaco2007 Feb]Cow Party
Time Limit: 5 Sec Memory Limit: 64 MBDescription
农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前去X牛棚参加派对还是返回住所,她们都采用了用时最少的路线.那么,用时最多的奶牛需要多少时间来回呢?Input
第1行:三个用空格隔开的整数.第2行到第M+1行,每行三个用空格隔开的整数:Ai, Bi,以及Ti.表示一条道路的起点,终点和需要花费的时间.
Output
唯一一行:一个整数: 所有参加聚会的奶牛中,需要花费总时间的最大值.Sample Input
4 8 21 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
10HINT
样例说明:共有4只奶牛参加聚会,有8条路,聚会位于第2个农场.
第4只奶牛可以直接到聚会所在地(花费3时间),然后返程路线经过第1和第3个农场(花费7时间),总共10时间.
Source
Silver题意:
有n个点,m条边,给出编号为x的点,让你求所有点到x然后返回的距离最小值最大是多少。一言不合上样例:
图中有4个点,连了8条边,奶牛们要前往2号点聚会;
1——>2 距离4 2——>1 距离1 2——>2 距离0 2——>2 距离0 3——>2 3——>1——>2 距离6 2——>3 2——>1——>3 距离3 4——>2 距离3 2——>4 2——>1——>3——>4 距离7
由上表可知
在第四个农场的奶牛前往2号点的距离+返回的距离最多
所以输出这个距离为3+7=10
题解
首先第一点,妥妥的最短路,直接上SPFA又考虑到题目要求奶牛来回的最短路径,所以我们可以反向建边
然后正着跑一遍反着跑一遍水过,1AC
直接上代码(通俗易懂)
#include<bits/stdc++.h> #define F(i,j,n) for(register int i=j;i<=n;i++) using namespace std; struct hahaha{ int from,to,v,nxt; }s[2][100100]; int n,m,x; int cnt[100100],head[2][100100],dis[2][1100],l[1100]; bool vis[1100]; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} return x*f; } inline int ins(int kind,int from,int to,int v){ cnt[kind]++; s[kind][cnt[kind]].from=from; s[kind][cnt[kind]].to=to; s[kind][cnt[kind]].v=v; s[kind][cnt[kind]].nxt=head[kind][from]; head[kind][from]=cnt[kind]; } void spfa(int kind){ F(i,1,n) dis[kind][i]=1e9; memset(vis,0,sizeof(vis)); int h=0,t=1; dis[kind][x]=0; l[1]=x; while(h!=t){ int now=l[++h]; if(h==1100) h=0; vis[now]=0; for(register int i=head[kind][now];i;i=s[kind][i].nxt){ if(dis[kind][now]+s[kind][i].v<dis[kind][s[kind][i].to]){ dis[kind][s[kind][i].to]=dis[kind][now]+s[kind][i].v; if(!vis[s[kind][i].to]){ vis[s[kind][i].to]=1; l[++t]=s[kind][i].to; if(t==1100) t=0; } } } } } int main(){ n=read();m=read();x=read(); F(i,1,m){ int from=read(),to=read(),v=read(); ins(0,from,to,v);ins(1,to,from,v); } spfa(0); spfa(1); int ans=0; F(i,1,n) ans=max(ans,dis[0][i]+dis[1][i]); printf("%d\n",ans); return 0; }
(阅读完本文了?在下方给个顶呗)
相关文章推荐
- 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)
- poj 3268 && bzoj 1631: [Usaco2007 Feb]Cow Party(最短路)
- BZOJ 1631: [Usaco2007 Feb]Cow Party
- [BZOJ] 1631: [Usaco2007 Feb]Cow Party
- bzoj 1631: [Usaco2007 Feb]Cow Party【spfa】
- BZOJ 1631: [Usaco2007 Feb]Cow Party( 最短路 )
- 【BZOJ 1631】 [Usaco2007 Feb]Cow Party
- BZOJ 1631: [Usaco2007 Feb]Cow Party spfa
- BZOJ1631 [Usaco2007 Feb]Cow Party
- BZOJ 1631: [Usaco2007 Feb]Cow Party
- BZOJ1631: [Usaco2007 Feb]Cow Party 最短路 Spfa
- bzoj1631 [Usaco2007 Feb]Cow Party
- bzoj1631 [Usaco2007 Feb]Cow Party(Dijkstra)
- BZOJ 1631==USACO 2007== POJ 3268 Cow Party奶牛派对
- [Poj 3268][Usaco2007 Feb]Cow Party奶牛派对
- 【动态规划】bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典
- BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典
- bzoj1631: [Usaco2007 Feb]Cow Party
- BZOJ1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典
- bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典