BZOJ 2015: [Usaco2010 Feb]Chocolate Giving spfa
2017-05-27 11:06
363 查看
2015: [Usaco2010 Feb]Chocolate Giving
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 450 Solved: 292
[Submit][Status][Discuss]
Description
Farmer John有B头奶牛(1<=B<=25000),有N(2*B<=N<=50000)个农场,编号1-N,有M(N-1<=M<=100000)条双向边,第i条边连接农场R_i和S_i(1<=R_i<=N;1<=S_i<=N),该边的长度是L_i(1<=L_i<=2000)。居住在农场P_i的奶牛A(1<=P_i<=N),它想送一份新年礼物给居住在农场Q_i(1<=Q_i<=N)的奶牛B,但是奶牛A必须先到FJ(居住在编号1的农场)那里取礼物,然后再送给奶牛B。你的任务是:奶牛A至少需要走多远的路程?Input
第1行:三个整数:N,M,B。第2..M+1行:每行三个整数:R_i,S_i和L_i,描述一条边的信息。
第M+2..M+B+1行:共B行,每行两个整数P_i和Q_i,表示住在P_i农场的奶牛送礼物给住在Q_i农场的奶牛。
Output
样例输出:共B行,每行一个整数,表示住在P_i农场的奶牛送礼给住在Q_i农场的奶牛至少需要走的路程
Sample Input
6 7 31 2 3
5 4 3
3 1 1
6 1 9
3 4 2
1 4 4
3 2 2
2 4
5 1
3 6
Sample Output
66
10
spfa()
ans=dis[x]+dis[y]
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f*x;
}
const int N=50100;
int n,m,ecnt,last
,dis
,q
;
bool book
;
struct EDGE{int to,nt,val;}e[N<<4];
inline void readd(int u,int v,int val)
{e[++ecnt]=(EDGE){v,last[u],val};last[u]=ecnt;}
inline void add(int u,int v,int val)
{readd(u,v,val);readd(v,u,val);}
void spfa()
{
memset(dis,0X7f,sizeof(dis));
int head=0,tail=1;q[0]=1;dis[1]=0;book[1]=1;
while(head!=tail)
{
int u=q[head++];book[u]=0;if(head==N-2)head=0;
for(int i=last[u];i;i=e[i].nt)
if(dis[e[i].to]>dis[u]+e[i].val)
{
dis[e[i].to]=dis[u]+e[i].val;
if(!book[e[i].to]){book[e[i].to]=1;q[tail++]=e[i].to;if(tail==N-2)tail=0;}
}
}
}
int main()
{
n=read();m=read();int k=read();
int u,v,val;
for(int i=1;i<=m;i++){u=read();v=read();val=read();add(u,v,val);}
spfa();
while(k--)
{
u=read();v=read();
printf("%d\n",dis[u]+dis[v]);
}
return 0;
}
/*
6 7 3
1 2 3
5 4 3
3 1 1
6 1 9
3 4 2
1 4 4
3 2 2
2 4
5 1
3 6
Sample Output
6
6
10
*/
相关文章推荐
- bzoj 2015: [Usaco2010 Feb]Chocolate Giving【spfa】
- 【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)
- bzoj 2015: [Usaco2010 Feb]Chocolate Giving
- [BZOJ2015][Usaco2010 Feb]Chocolate Giving
- BZOJ 2015: [Usaco2010 Feb]Chocolate Giving( 最短路 )
- USACO 2010 Feb Chocolate Giving 领取巧克力
- bzoj2015 [Usaco2010 Feb]Chocolate Giving
- bzoj2015 [Usaco2010 Feb]Chocolate Giving
- bzoj2015 [Usaco2010 Feb]Chocolate Giving(最短路裸题)
- 【bzoj3942】 [Usaco2015 Feb]Censoring KMP
- bzoj2014 [Usaco2010 Feb]Chocolate Buying
- BZOJ 3940: [Usaco2015 Feb]Censoring
- BZOJ 3943 Usaco2015 Feb SuperBull Prim
- bzoj 3943: [Usaco2015 Feb]SuperBull 最大生成树
- bzoj 3889: [Usaco2015 Jan]Cow Routing SPFA
- bzoj 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘 spfa+bfs
- 2015: [Usaco2010 Feb]Chocolate Giving
- [bzoj3943][Usaco2015 Feb]SuperBull_Kruskal
- BZOJ 1782: [Usaco2010 Feb]slowdown 慢慢游( BIT + dfs )
- BZOJ 3940 [Usaco2015 Feb]Censoring