1674: [Usaco2005]Part Acquisition

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 337  Solved: 162
[Submit][Status][Discuss]

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

Silver

题解:

dijkstra+堆优化。

连边,跑最短路。。。

 #include<bits/stdc++.h>
using namespace std;
#define INF 1e9
#define MAXK 1010
#define MAXN 50010
struct node
{
int begin,end,value,next;
}edge[MAXN];
int cnt,Head[MAXK],dis[MAXK],SIZE=,Heap[MAXK],K,pos[MAXK];
void addedge(int bb,int ee,int vv)
{
edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].value=vv;edge[cnt].next=Head[bb];Head[bb]=cnt;
}
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void Push1(int k)
{
int now=k,root;
while(now>)
{
root=now/;
if(dis[Heap[root]]<=dis[Heap[now]])return;
swap(Heap[root],Heap[now]);
swap(pos[Heap[root]],pos[Heap[now]]);
now=root;
}
}
void Insert(int k)
{
Heap[++SIZE]=k;pos[k]=SIZE;Push1(SIZE);
}
void Pop1(int k)
{
int now,root=k;
pos[Heap[k]]=;Heap[k]=Heap[SIZE--];if(SIZE>)pos[Heap[k]]=k;
while(root<=SIZE/)
{
now=root*;
if(now<SIZE&&dis[Heap[now+]]<dis[Heap[now]])now++;
if(dis[Heap[root]]<=dis[Heap[now]])return;
swap(Heap[root],Heap[now]);
swap(pos[Heap[root]],pos[Heap[now]]);
root=now;
}
}
int dijkstra(int start)
{
int i,u,v;
for(i=;i<=K;i++)dis[i]=INF;dis[start]=;
for(i=;i<=K;i++)Insert(i);
while(SIZE>)
{
u=Heap[];Pop1(pos[u]);
for(i=Head[u];i!=-;i=edge[i].next)
{
v=edge[i].end;
if(dis[u]+edge[i].value<dis[v]){dis[v]=dis[u]+edge[i].value;Push1(pos[v]);}
}
}
return dis[K];
}
int main()
{
int n,bb,ee,i,ans;
n=read();K=read();
memset(Head,-,sizeof(Head));cnt=;
for(i=;i<=n;i++)
{
bb=read();ee=read();addedge(bb,ee,);
}
ans=dijkstra();
if(ans==INF)printf("-1");
else printf("%d",ans);
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 MB Description The cows have b ...

  2. Bzoj 2834: 回家的路 dijkstra,堆优化,分层图,最短路

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

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

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

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

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

  5. Bzoj 2662: [BeiJing wc2012]冻结 dijkstra,堆,分层图,最短路

    2662: [BeiJing wc2012]冻结 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 647  Solved: 348[Submit][Sta ...

  6. BZOJ1674: [Usaco2005]Part Acquisition

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

  7. Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 969  Solved: 468[S ...

  8. Bzoj 1975: [Sdoi2010]魔法猪学院 dijkstra,堆,A*,K短路

    1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1357  Solved: 446[Submit][Statu ...

  9. Bzoj 1598: [Usaco2008 Mar]牛跑步 dijkstra,堆,K短路,A*

    1598: [Usaco2008 Mar]牛跑步 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 427  Solved: 246[Submit][St ...

随机推荐

  1. ubuntu 安装qt 5.1的各种错误

    错误太多了,我就不一一说 了,直接一条命令搞定 sudo apt-:i386 libx11-:i386 libglib2.-:i386 libfreetype6:i386 libSM6:i386 li ...

  2. C++ 智能指针auto_ptr详解

    1. auto_ptr 的设计动机: 函数操作经常依照下列模式进行: 获取一些资源 执行一些动作 释放所获取的资源 那么面对这些资源的释放问题就会出现下面的两种情况: 一开始获得的资源被绑定于局部对象 ...

  3. EF 存储过程(下)

    本节,我们将学习如何手动添加/修改存储过程,如何使EF能够支持Output类型的参数 > 添加/修改存储过程 有时候,某个SQL语句比较复杂,但是数据库中又没有定义相应的存储过程.这个时候,我们 ...

  4. sea.js,spm学习

    安装spm 下载sea.js 运行spm npm install spm@2.x -g npm install spm-build -g 下载sea.js git clone https://gith ...

  5. ConfigParser---python

    # !/usr/bin/python # Filename:tcfg.py import ConfigParser config = ConfigParser.ConfigParser() confi ...

  6. 黑马程序员-------.net基础知识五

    方法(函数) 作用:用来重复代码,当我们在一个过程中反复的写了同样的代码,一般情况下,我们就可以把需要重复写的代码定义在方法中,用的时候只需调用即可 语法: [访问修饰符][static] 返回值类型 ...

  7. POJ 1961 Period KMP算法next数组的应用

    题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...

  8. 自定义modal一个控制器的效果, presentViewController

    presentViewController 一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UI ...

  9. scanf格式控制符

    格式控制 . %d %o %x %c %s %f %e 无%u格式.%g格式 . scanf("%3d%3d", &a, &b); 输入: //a=123,b=45 ...

  10. <c:if>判断参数是否为空

    EL表达式判断字符串是否为空: <c:if test="${empty str}"> str为空</c:if> <c:if test="${ ...