[Usaco2005][BZOJ1674] Part Acquisition|dijkstra|priority_queue

1674: [Usaco2005]Part Acquisition

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 308  Solved: 143
[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<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>
#define inf 1000000000
#define pa pair<int,int>
using namespace std;
int n,m,cnt,dis[1005],head[1005];
bool vis[1005];
int next[50005],list[50005],key[50005];
inline int read()
{
    int a=0,f=1; char c=getchar();
    while (c<‘0‘||c>‘9‘) {if (c==‘-‘) f=-1; c=getchar();}
    while (c>=‘0‘&&c<=‘9‘) {a=a*10+c-‘0‘; c=getchar();}
    return a*f;
}
inline void insert(int u,int v,int w)
{
    next[++cnt]=head[u];
    head[u]=cnt;
    list[cnt]=v;
    key[cnt]=w;
}
inline void dijkstra()
{
    priority_queue<pa,vector<pa>,greater<pa> > q;
    for (int i=1;i<=n;i++) dis[i]=inf,vis[i]=0;
    dis[1]=0;
    q.push(make_pair(0,1));
    while (!q.empty())
    {
        int now=q.top().second; q.pop();
        if (vis[now]) continue; vis[now]=1;
        for (int i=head[now];i;i=next[i])
        {
            if (dis[list[i]]>dis[now]+key[i])
            {
                dis[list[i]]=dis[now]+key[i];
                q.push(make_pair(dis[list[i]],list[i]));
            }
        }
    }
}
int main()
{
    m=read(); n=read();
    for (int i=1;i<=m;i++)
    {
        int u=read(),v=read();
        insert(u,v,1);
    }
    dijkstra();
    if (dis[n]==inf) puts("-1"); else printf("%d",dis[n]+1);
    return 0;
}
时间: 2024-08-28 12:53:57

[Usaco2005][BZOJ1674] Part Acquisition|dijkstra|priority_queue的相关文章

BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易

3392: [Usaco2005 Feb]Part Acquisition 交易 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 26  Solved: 18[Submit][Status] Description 奶牛们接到了寻找一种新型挤奶机的任务,为此它们准备依次经过N(1≤N≤50000)颗行星,在行星上进行交易.为了方便,奶牛们已经给可能出现的K(1≤K≤1000)种货物进行了由1到K的标号.由于这些行星都不是十分发达.没有流通的货币,所以

BOI&#39;98 DAY 2 TASK 1 CONFERENCE CALL Dijkstra/Dijkstra+priority_queue/SPFA

BOI'98 DAY 2 TASK 1 CONFERENCE CALL PROBLEM A telecom company would like to offer a three-party conference call service. This service enables three customers to participate in a telephone conversation simultaneously. A customer accesses the interconn

dijkstra+priority_queue+vector

最短路 时间限制: 3 Sec  内存限制: 128 MB 题目描述 给定M条边,N个点的带权无向图 求1到N的最短路 N<=100000 M<=500000 输入 第一行:N,M 接下来M行3个正整数:ai,bi,ci 表示ai,bi之间有一条长度为ci的路 0<=ci<=1000 输出 一个整数,表示1到N的最短距离 样例输入 4 4 1 2 1 2 3 1 3 4 1 2 4 1 样例输出 2 提示 注意图中可能有重边和自环,数据保证1到N有路径相连 1 #include&l

Dijkstra 优先队列优化

#include <iostream> #include <queue> #include <vector> using namespace std; const int N=405; struct rec { int v,w; }; vector<rec> edge[N*N]; int n,st,ed; __int64 dis[N*N]; bool vis[N*N]; struct cmp { bool operator()(int a,int b) {

P103 单源最短路问题 第三种Dijkstra算法

///通过队列,找出最小的dis[]值,取出配对的vertex值. /// stack priority_queue set struct edge { int to; int cost; } ; vector <edge>G[MZX_V];///表 typedef pair<int ,int > Pa;///Pa.second是点的编号 Pa.first是到该点的最短距离 int dis[MAX_V]; void Dijkstra( ) { priority_queue<

658 - It&#39;s not a Bug, it&#39;s a Feature! (Dijkstra算法)

今天第一次系统的学习了一下最短路算法,开始刷第十一章,第一次写Dijkstra算法,出现了很多喜闻乐见的错误..而且uva上样例很水,瓢虫也很水 ,坑了我好久. 首先是对于结点的处理,我们必须要维护一个二元组,一个表示结点一个表示当前结点最短路.   因为Dijkstra算法利用了优先队列来加速算法,所以需要定义小于运算符,一开始我直接将状态装进了优先队列,显然是不对的,因为优先队列的作用就是取出当前距离最短的结点. 其次,说说最短路算法蕴含的巧妙思想: 每次从当前所有还未标记的结点中选择一个距

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA)

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n,树根为编号1,选择一些边,使得所有节点构成一棵树,选择边的代价是(子孙的点的重量)×(这条边的价值).求代价最小多少. 分析: 单看每个点被计算过的代价,很明显就是从根到节点的边的价值.所以这是个简单的单源最短路问题. 不过坑点还是很多的. 点的数量高达5w个,用矩阵存不行,只能用边存. 还有路径和结

POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Accepted: 15899 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for

POJ 2253 Frogger(dijkstra)

传送门 Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39453   Accepted: 12691 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit