bzoj 1674: [Usaco2005]Part Acquisition -- dijkstra(堆优化)
1674: [Usaco2005]Part Acquisition
Time Limit: 5 Sec Memory Limit: 64 MBDescription
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.
这些牛被派去太空任务,为他们的谷仓购置一台新挤奶机。他们是通过一组包含N(1 < = N = 50000)行星,每一个交易岗位的恒星飞行。奶牛已经确定的K(1 < = K = 1000)类型的对象(编号为1 K)每个行星在集群的欲望,以及他们必须交易的产品。没有行星发展货币,所以他们在易货制度下工作:所有交易由每一方交易完全一个对象(大概是不同类型)。奶牛从地球开始与高品质的干草筒(项目1),他们希望一个新的挤奶机(项目k)。帮助他们找到最好的方法,使一系列的交易在行星的集群,以获得项目K.如果这个任务是不可能的,输出- 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
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.
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
题目就是相当于有n条有向边,边权为1,求1到k的最短路
#include<map> #include<cmath> #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define ll long long #define M 50010 #define N 1010 #define pa pair<int,int> 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=x*10+ch-'0';ch=getchar();} return x*f; } int cnt,lj[N],fro[M],to[M],v[M]; void add(int a,int b,int w){fro[++cnt]=lj[a];to[cnt]=b;v[cnt]=w;lj[a]=cnt;} int dis[N]; bool vs[N]; void dijkstra() { priority_queue<pa,vector<pa>,greater<pa> >q; memset(dis,127,sizeof(dis)); dis[1]=0; q.push(make_pair(0,1)); int u; while(!q.empty()) { u=q.top().second;q.pop(); for(int i=lj[u];i;i=fro[i]) { if(dis[to[i]]>dis[u]+v[i]) { dis[to[i]]=dis[u]+v[i]; q.push(make_pair(dis[to[i]],to[i])); } } } } int n,k,x,y; int main() { n=read();k=read(); for(int i=1;i<=n;i++) { x=read();y=read(); add(x,y,1); } dijkstra(); dis[k]>100000000?puts("-1"):printf("%d\n",dis[k]+1); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。