1674: [Usaco2005]Part Acquisition

1674: [Usaco2005]Part Acquisition

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 397   Solved: 191
[ Submit][ Status][ Discuss]

Description

The cows have been sent on a mission through space to acquire a new milking machine for their barn. They are flying through a cluster of stars containing N (1 <= N <= 50,000) planets, each with a trading post. The cows have determined which of K (1 <= K <= 1,000) types of objects (numbered 1..K) each planet in the cluster desires, and which products they have to trade. No planet has developed currency, so they work under the barter system: all trades consist of each party trading exactly one object (presumably of different types). The cows start from Earth with a canister of high quality hay (item 1), and they desire a new milking machine (item K). Help them find the best way to make a series of trades at the planets in the cluster to get item K. If this task is impossible, output -1.

Input

* Line 1: Two space-separated integers, N and K. * Lines 2..N+1: Line i+1 contains two space-separated integers, a_i and b_i respectively, that are planet i's trading trading products. The planet will give item b_i in order to receive item a_i.

Output

* Line 1: One more than the minimum number of trades to get the milking machine which is item K (or -1 if the cows cannot obtain item K).

Sample Input

6 5 //6个星球,希望得到5,开始时你手中有1号货物.
1 3 //1号星球,希望得到1号货物,将给你3号货物
3 2
2 3
3 1
2 5
5 4

Sample Output

4


OUTPUT DETAILS:

The cows possess 4 objects in total: first they trade object 1 for
object 3, then object 3 for object 2, then object 2 for object 5.

HINT

Source

Silver



最短路不解释。。。

写了两份代码,本意是想练习dij的,平常不太用,然后wa了,就改写spfa,AC了

发现dij的优先队列优先级定义错了,改掉AC。

发现dij好像比spfa快。

截图:



dij代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<climits>
#include<string>
#include<cmath>
#include<climits>
#include<queue>
#include<cstdio>
#include<cstdlib>
#define N 50005
#define M 100001
#define inf 0x3f3f3f3f
using namespace std;
int n,g;
int head[N],pos;
struct edge{int to,next,c;}e[M];
void add(int a,int b)
{pos++;e[pos].to=b,e[pos].next=head[a],e[pos].c=1;head[a]=pos;}
struct node{int u,d;}s,t;
bool operator < (node a,node b){return a.d>b.d;}
bool done[N];
priority_queue<node>Q;
int d[N];
void dij()
{
	for(int i=1;i<=n;i++)done[i]=0,d[i]=inf;
	s.u=1,s.d=0;Q.push(s);d[s.u]=0;
	while(!Q.empty())
		{
			s=Q.top();Q.pop();
			if(done[s.u])continue;
			done[s.u]=1;
			for(int i=head[s.u];i;i=e[i].next)
				{
					int v=e[i].to;
					if(d[v]>s.d+e[i].c)
						{
							d[v]=s.d+e[i].c;
							t.u=v,t.d=d[v];
							Q.push(t);
						}
				}
		}
}
int main()
{
	scanf("%d%d",&n,&g);
	for(int i=1,a,b;i<=n;i++)
		{
			scanf("%d%d",&a,&b);
			add(a,b);
		}dij();
	if(d[g]!=inf)printf("%d\n",d[g]+1);
	else printf("-1\n");
}


spfa代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<climits>
#include<string>
#include<cmath>
#include<climits>
#include<queue>
#include<cstdio>
#include<cstdlib>
#define N 50005
#define M 100001
#define inf 0x3f3f3f3f
using namespace std;
int n,g;
int head[N],pos;
struct edge{int to,next,c;}e[M];
void add(int a,int b)
{pos++;e[pos].to=b,e[pos].next=head[a],e[pos].c=1;head[a]=pos;}
queue<int>Q;bool vis[N];int d[N];
void spfa()
{
	for(int i=1;i<=n;i++)vis[i]=0,d[i]=inf;
	d[1]=0,vis[1]=1;Q.push(1);
	while(!Q.empty())
		{
			int u=Q.front();Q.pop();vis[u]=0;
			for(int i=head[u];i;i=e[i].next)
				{
					int v=e[i].to;
					if(d[v]>d[u]+e[i].c)
						{
							d[v]=d[u]+e[i].c;
							if(!vis[v])
								{
									vis[v]=1;
									Q.push(v);
								}
						}	
				}
		}
}
int main()
{
	scanf("%d%d",&n,&g);
	for(int i=1,a,b;i<=n;i++)
		{
			scanf("%d%d",&a,&b);
			add(a,b);
		}spfa();
	if(d[g]==inf)printf("-1\n");
	else printf("%d\n",d[g]+1);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值