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. C# 开发(创蓝253)手机短信验证码接口

    创蓝253: https://www.253.com/ #region 获取手机验证码(创蓝253) /// <summary> /// 获取手机验证码(创蓝253) /// </s ...

  2. 访问公网WebService服务

    接下来,我们演示如何访问公网webservice服务. 我们以访问 http://www.webxml.com.cn/zh_cn/index.aspx 为例,主要演示手机号码归属地查询服务(使用说明书 ...

  3. xpath简单应用

    相对路径与绝对路径: 如果"/"处在XPath表达式开头则表示文档根元素,(表达式中间作为分隔符用以分割每一个步进表达式)如:/messages/message/subject是一 ...

  4. MVC – 15.路由机制

    15.1.路由检测插件 - RouteDebug 15.2.路由约束 15.3.命名路由 15.4.验证码 15.5.ASP.NET MVC 与 三层架构 15.6.Area区域 15.6.1.尝试将 ...

  5. go中操作json

    package main import ( "encoding/json" "fmt" ) type Server struct { ServerName st ...

  6. 【PAT】1001. 害死人不偿命的(3n+1)猜想 (15)

    1001. 害死人不偿命的(3n+1)猜想 (15) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去, ...

  7. python存取数据进阶技巧-pickle,array模块

    我们在存/取数据时,没有必要存成文本形式,多试试二进制形式,文本只是骗骗眼睛的,要更快和更高效 1.数组形式 如果我们需要一个之包含数字的列表,那就试试array.array,注意,不是numpy模块 ...

  8. Python的hasattr() getattr() setattr() 函数使用方法(简介)

    hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False.需要注意的是name要用括号括起来 1 ...

  9. css绝对居中img

    html: <div id="imgs"> <img src="http://pic.616pic.com/ys_b_img/00/03/60/Kt6Q ...

  10. ref:mysql命令大全

    Mysql常用命令行大全 ref:https://www.cnblogs.com/bluealine/p/7832219.html 1)查看表结构:desc table_name; 2)查看创建表的s ...