1674: [Usaco2005]Part Acquisition

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

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

题解:
我会说我没看题就是为了写个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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,m,tot;
int d[],head[];
bool v[];
struct edge{int go,next,w;}e[];
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=;i<=n;i++)d[i]=inf;
memset(v,,sizeof(v));
d[]=;q.push(make_pair(,));
while(!q.empty())
{
int x=q.top().second;q.pop();
if(v[x])continue;v[x]=;
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=;i<=m;i++)
{
int x=read(),y=read(),z=;
insert(x,y,z);
}
dijkstra();
if(d[n]==inf)puts("-1");
else printf("%d",d[n]+);
return ;
}

BZOJ1674: [Usaco2005]Part Acquisition的更多相关文章

  1. Bzoj 1674: [Usaco2005]Part Acquisition dijkstra,堆

    1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 337  Solved: 162[Sub ...

  2. bzoj 1674: [Usaco2005]Part Acquisition -- dijkstra(堆优化)

    1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec  Memory Limit: 64 MB Description The cows have b ...

  3. 【BZOJ】1674: [Usaco2005]Part Acquisition(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1674 想法很简单...将每一种看做一个点,如果i可以换成j,那么连边到j.. 费用都为1.. 然后拥 ...

  4. [Usaco2005]Part Acquisition

    Description The cows have been sent on a mission through space to acquire a new milking machine for ...

  5. usaco silver

    大神们都在刷usaco,我也来水一水 1606: [Usaco2008 Dec]Hay For Sale 购买干草   裸背包 1607: [Usaco2008 Dec]Patting Heads 轻 ...

  6. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  7. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  8. BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易

    3392: [Usaco2005 Feb]Part Acquisition 交易 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 26  Solved:  ...

  9. bzoj:3392: [Usaco2005 Feb]Part Acquisition 交易

    Description     奶牛们接到了寻找一种新型挤奶机的任务,为此它们准备依次经过N(1≤N≤50000)颗行星,在行星上进行交易.为了方便,奶牛们已经给可能出现的K(1≤K≤1000)种货物 ...

随机推荐

  1. Ubuntu 下 JDK+Tomcat+MySql 环境的搭建

    Linux环境 修改catalina.sh 在“echo "Using CATALINA_BASE: $CATALINA_BASE"”上面加入以下行: JAVA_OPTS=&quo ...

  2. [Angular 2] NgNonBindable

    If you want to print someting like {{content}} on the html, using ng-non-bindable directive: <div ...

  3. android 时间对话框 TimePickerDialog简介

     个人也提醒功能的时候用到了TimePickerDialog对话框,查阅了非常多技术资料,可是感觉非常多东西都说的不是非常具体,而且非常多地方.都有不完好的地方.比方有弹出对话框得到的不是系统当前 ...

  4. linux下查看磁盘空间

    如果要查看磁盘还剩多少空间,当然是用df的命令了. [root@localhost ~]# df -h  文件系统              容量 已用 可用 已用% 挂载点  /dev/sda2   ...

  5. 再回首,Java温故知新(二):Java基本数据类型

    Java作为一种强类型语言,意味着每一个变量都会有特定的类型,Java共有8种基本类型,其中有4种整型(byte.short.int.long).两种浮点型(float.double).1种字符型(c ...

  6. c++回调编程本质

    1. boost:bind获得一个函数对象,就像函数指针一样,这个行为可以作为回调 2. bosot:bind的函数对象可以保存别的对象的引用,回调对象的成员函数 3. boost:function是 ...

  7. js获取当前url地址及参数

    介绍:设置或获取对象指定的文件名或路径. window.location.pathname //返回 设置或获取整个 URL 为字符串. window.location.href 设置或获取与 URL ...

  8. 编写高效SQL最佳实践

    编写高效 SQL 语句的最佳实践 秦玮, 高级软件工程师, IBM 王广成, 软件工程师, IBM 王韵婷, 高级软件工程师, IBM 简介: 本文列举了一些在编写 SQL 查询语句时可能导致 DB2 ...

  9. jQuery ajax - serialize() 方法

    http://www.jb51.net/article/60849.htm http://www.w3school.com.cn/jquery/ajax_serialize.asp

  10. eclipse/myeclipse 变量名自动补全问题

    原理是:在输入变量名后,去掉按下空格或=后,代码上屏  以前只知道alt+/调出assist,后来发现可以所有字母都激活content assist(8.1里有写).用起来果然很爽,但是eclipse ...