1674: [Usaco2005]Part Acquisition

Time Limit: 5 Sec  Memory Limit: 64 MB

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.
 
这些牛被派去太空任务,为他们的谷仓购置一台新挤奶机。他们是通过一组包含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

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

题目就是相当于有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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';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,,sizeof(dis));
dis[]=;
q.push(make_pair(,));
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=;i<=n;i++)
{
x=read();y=read();
add(x,y,);
}
dijkstra();
dis[k]>?puts("-1"):printf("%d\n",dis[k]+);
return ;
}

bzoj 1674: [Usaco2005]Part Acquisition -- dijkstra(堆优化)的更多相关文章

  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 2834: 回家的路 dijkstra,堆优化,分层图,最短路

    2834: 回家的路 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 62  Solved: 38[Submit][Status][Discuss] D ...

  3. POJ 2502 - Subway Dijkstra堆优化试水

    做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...

  4. Bzoj 2346: [Baltic 2011]Lamp dijkstra,堆

    2346: [Baltic 2011]Lamp Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 428  Solved: 179[Submit][Sta ...

  5. POJ2387(dijkstra堆优化)

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...

  6. hdu 2544 单源最短路问题 dijkstra+堆优化模板

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  7. 深入理解dijkstra+堆优化

    深入理解dijkstra+堆优化 其实就这几种代码几种结构,记住了完全就可以举一反三,所以多记多练多优化多思考. Dijkstra   对于一个有向图或无向图,所有边权为正(边用邻接矩阵的形式给出), ...

  8. dijkstra堆优化(multiset实现->大大减小代码量)

    例题: Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣 ...

  9. POJ 1511 - Invitation Cards 邻接表 Dijkstra堆优化

    昨天的题太水了,堆优化跑的不爽,今天换了一个题,1000000个点,1000000条边= = 试一试邻接表 写的过程中遇到了一些问题,由于习惯于把数据结构封装在 struct 里,结果 int [10 ...

随机推荐

  1. react 修改state某一属性值

    1.state // 筛选框相关数据 searchSelect: { term: { value: '学期', key: '', options: [] }, type_of_personnel: { ...

  2. bzoj 3123 可持久化线段树启发式合并

    首先没有连边的操作的时候,我们可以用可持久化线段树来维护这棵树的信息,建立权值可持久化线段树,那么每个点继承父节点的线段树,当询问为x,y的时候我们可以询问rot[x]+rot[y]-rot[lca( ...

  3. 64_m3

    molequeue-doc-0.8.0-2.20161222giteb397e.fc26.no..> 05-Apr-2017 10:04 451570 molequeue-libs-0.8.0- ...

  4. screen命令使用方法【转】

    在linux的环境中,我们想要在后台持续运行一些脚本,但是又因为关闭这个tty的话,脚本就会中断,这个时候我们就需要screen这个工具的帮助啦! 基础   1 首先先查看下否则有这个工具.如果运行s ...

  5. 2017 CERC

    2017 CERC Problem A:Assignment Algorithm 题目描述:按照规则安排在飞机上的座位. solution 模拟. 时间复杂度:\(O(nm)\) Problem B: ...

  6. vsts 自动部署到Azure

    如果要部署到中国区的Azure ,请先阅读 http://www.cnblogs.com/cnryb/p/7867275.html 前置条件,我把代码托管在vsts(放在GitHub上也没问题,这里不 ...

  7. 自动ftp 上传

    #!/bin/sh ftp -n<<END_FTP open 192.168.1.4 user codfei duibuqi //用户名codfei 密码duibuqi binary pr ...

  8. apache 各种配置

    //apache 的网站配置文件 /usr/local/apache2/conf/extra/httpd-vhosts.conf -->在编辑这个文件前需要去httpd.conf把这个文件的注释 ...

  9. C/C++——[04] 语句

    在 C/C++语言中,语句以“ :”结束.某些情况下,一组语句在一起共同完成某一特定的功能,可以将它们用大括号括起来.我们称之为语句组.语句组可以出现在任何单个语句出现的地方. 1. 分支语句 一般情 ...

  10. mysql cursor游标的使用,实例

    mysql被oracle收购后,从mysql-5.5开始,将InnoDB作为默认存储引擎,是一次比较重大的突破.InnoDB作为支持事务的存储引擎,拥有相关的RDBMS特性:包括ACID事务支持,数据 ...