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. JSON.stringify()——JS转JSON字符串

    JSON.stringify() JSON 通常用于与服务端交换数据. 在向服务器发送数据时一般是字符串. 我们可以使用 JSON.stringify() 方法将 JavaScript 对象转换为字符 ...

  2. 9 - Python函数定义-位置参数-返回值

    目录 1 函数介绍 1.1 为什么要使用函数 1.2 Python中的函数 2 函数的基本使用 3 函数的参数 3.1 参数的默认值 3.2 可变参数 3.2.1 可变位置传参 3.2.2 可变关键字 ...

  3. C++中string.find()函数,string.find_first_of函数与string::npos

    查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA. ...

  4. python实战===python程序打包成exe

    推荐PyInstaller项目www.pyinstaller.org   安装方法: 先跑pip install pywin32再跑pip install pyinstaller即可 可用一句命令打包 ...

  5. 新一代的USB 3.0传输规格

    通用序列总线(USB) 从1996问世以来,一统个人电脑外部连接界面,且延伸至各式消费性产品,早已成为现代人生活的一部分.2000年发表的USB 2.0 High-speed规格,提供了480Mbps ...

  6. MySQL常见错误代码说明

    附:MySQL常见错误代码说明 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据 ...

  7. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...

  8. Linux软件管理器(如何使用软件管理器来管理软件)2---安装及管理Linux应用程序

    安装及管理Linux应用程序 Linux应用程序的组成1.普通的可执行程序文件,一般保存在/usr/bin目录中,普通用户即可执行.2.服务器程序.管理程序文件,一般保存在/usr/sbin目录中,需 ...

  9. LightOJ - 1297 Largest Box LightOJ(一元三次方程求极大值)

    题目链接:https://vjudge.net/contest/28079#problem/K 题目大意:给你一个长为L,宽为W的纸片,四个角剪掉边长为x的正方形,如下图所示,然后折成一个无盖的纸盒, ...

  10. hdu 5001(概率DP)

    Walk Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...