Bzoj 1674: [Usaco2005]Part Acquisition dijkstra,堆

1674: [Usaco2005]Part Acquisition

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 337  Solved: 162
[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

 

题解:

dijkstra+堆优化。

连边,跑最短路。。。

 1 #include<bits/stdc++.h> 
 2 using namespace std; 
 3 #define INF 1e9 
 4 #define MAXK 1010 
 5 #define MAXN 50010 
 6 struct node 
 7 { 
 8     int begin,end,value,next; 
 9 }edge[MAXN]; 
10 int cnt,Head[MAXK],dis[MAXK],SIZE=0,Heap[MAXK],K,pos[MAXK]; 
11 void addedge(int bb,int ee,int vv) 
12 { 
13     edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].value=vv;edge[cnt].next=Head[bb];Head[bb]=cnt; 
14 } 
15 int read() 
16 { 
17     int s=0,fh=1;char ch=getchar(); 
18     while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();} 
19     while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();} 
20     return s*fh; 
21 } 
22 void Push1(int k) 
23 { 
24     int now=k,root; 
25     while(now>1) 
26     { 
27         root=now/2; 
28         if(dis[Heap[root]]<=dis[Heap[now]])return; 
29         swap(Heap[root],Heap[now]); 
30         swap(pos[Heap[root]],pos[Heap[now]]); 
31         now=root; 
32     } 
33 } 
34 void Insert(int k) 
35 { 
36     Heap[++SIZE]=k;pos[k]=SIZE;Push1(SIZE); 
37 } 
38 void Pop1(int k) 
39 { 
40     int now,root=k; 
41     pos[Heap[k]]=0;Heap[k]=Heap[SIZE--];if(SIZE>0)pos[Heap[k]]=k; 
42     while(root<=SIZE/2) 
43     { 
44         now=root*2; 
45         if(now<SIZE&&dis[Heap[now+1]]<dis[Heap[now]])now++; 
46         if(dis[Heap[root]]<=dis[Heap[now]])return; 
47         swap(Heap[root],Heap[now]); 
48         swap(pos[Heap[root]],pos[Heap[now]]); 
49         root=now; 
50     } 
51 } 
52 int dijkstra(int start) 
53 { 
54     int i,u,v; 
55     for(i=1;i<=K;i++)dis[i]=INF;dis[start]=1; 
56     for(i=1;i<=K;i++)Insert(i); 
57     while(SIZE>0) 
58     { 
59         u=Heap[1];Pop1(pos[u]); 
60         for(i=Head[u];i!=-1;i=edge[i].next) 
61         { 
62             v=edge[i].end; 
63             if(dis[u]+edge[i].value<dis[v]){dis[v]=dis[u]+edge[i].value;Push1(pos[v]);} 
64         } 
65     } 
66     return dis[K]; 
67 } 
68 int main() 
69 { 
70     int n,bb,ee,i,ans; 
71     n=read();K=read(); 
72     memset(Head,-1,sizeof(Head));cnt=1; 
73     for(i=1;i<=n;i++) 
74     { 
75         bb=read();ee=read();addedge(bb,ee,1); 
76     } 
77     ans=dijkstra(1); 
78     if(ans==INF)printf("-1"); 
79     else printf("%d",ans); 
80     return 0; 
81 }

 

posted @ 2016-03-27 21:09  微弱的世界  阅读(221)  评论(0编辑  收藏  举报