BZOJ1674: [Usaco2005]Part Acquisition
2014-08-21 17:00
127 查看
1674: [Usaco2005]Part Acquisition
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 259 Solved: 114
[Submit][Status]
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
4OUTPUT 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+heap的模版吗?
代码:
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<queue> #define inf 1000000000 #define maxn 50000+100 #define maxm 2000000 #define eps 1e-10 #define ll long long #define pa pair<int,int> 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>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} return x*f; } int n,m,tot; int d[1005],head[1005]; bool v[1005]; struct edge{int go,next,w;}e[50005]; void insert(int x,int y,int z) { e[++tot].go=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot; } void dijkstra() { priority_queue<pa,vector<pa>,greater<pa> >q; for(int i=1;i<=n;i++)d[i]=inf; memset(v,0,sizeof(v)); d[1]=0;q.push(make_pair(0,1)); while(!q.empty()) { int x=q.top().second;q.pop(); if(v[x])continue;v[x]=1; for(int i=head[x],y;i;i=e[i].next) if(d[x]+e[i].w<d[y=e[i].go]) { d[y]=d[x]+e[i].w; q.push(make_pair(d[y],y)); } } } int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); m=read();n=read(); for(int i=1;i<=m;i++) { int x=read(),y=read(),z=1; insert(x,y,z); } dijkstra(); if(d ==inf)puts("-1"); else printf("%d",d +1); return 0; }
View Code
相关文章推荐
- bzoj1680[Usaco2005 Mar]Yogurt factory
- bzoj1731[Usaco2005 dec]Layout 排队布局 差分约束
- bzoj1755 [Usaco2005 qua]Bank Interest
- bzoj1671 [Usaco2005 Dec]Knights of Ni 骑士
- bzoj1684 [Usaco2005 Oct]Close Encounter
- usaco 2005 奶牛大学(财政补助)
- 【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草
- 【BZOJ】1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚(dp/线段树)
- 【BZOJ】1674: [Usaco2005]Part Acquisition(spfa)
- bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群
- bzoj 1753: [Usaco2005 qua]Who's in the Middle【排序】
- BZOJ 1688: [Usaco2005 Open]Disease Manangement 疾病管理
- BZOJ1680: [Usaco2005 Mar]Yogurt factory
- bzoj 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+最大流
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
- bzoj 1688: [Usaco2005 Open]Disease Manangement 疾病管理(状压)
- 1676: [Usaco2005 Feb]Feed Accounting 饲料计算
- bzoj 1689: [Usaco2005 Open] Muddy roads 泥泞的路(模拟)
- 【最小生成树】BZOJ1682[Usaco2005 Mar]-Out of Hay 干草危机
- 1674: [Usaco2005]Part Acquisition