[Usaco2005]Part Acquisition
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号物品,每个星球会帮你把物品a换成物品b,问你要得到物品m最少要换几次,如果怎么样都不能换到,输出-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
这题裸的最短路,什么都不用想
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=5e4;
int pre[N+10],now[N+10],child[N+10];
int h[N+10],dis[N+10];
bool vis[N+10];
int tot,n,End,root=1;
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
void SPFA(int x){
int head=0,tail=1;
memset(dis,63,sizeof(dis));
h[1]=x,vis[x]=1,dis[x]=1;
while (head!=tail){
if (++head>N) head=1;
int Now=h[head];
for (int p=now[Now],son=child[p];p;p=pre[p],son=child[p])
if (dis[son]>dis[Now]+1){
dis[son]=dis[Now]+1;
if (!vis[son]){
if (++tail>N) tail=1;
h[tail]=son,vis[son]=1;
}
}
vis[Now]=0;
}
}
int main(){
n=read(),End=read();
for (int i=1,x,y;i<=n;i++) x=read(),y=read(),join(x,y);
SPFA(root);
dis[End]!=inf?printf("%d\n",dis[End]):printf("-1\n");
return 0;
}
[Usaco2005]Part Acquisition的更多相关文章
- Bzoj 1674: [Usaco2005]Part Acquisition dijkstra,堆
1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 337 Solved: 162[Sub ...
- BZOJ1674: [Usaco2005]Part Acquisition
1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 259 Solved: 114[Sub ...
- bzoj 1674: [Usaco2005]Part Acquisition -- dijkstra(堆优化)
1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec Memory Limit: 64 MB Description The cows have b ...
- 【BZOJ】1674: [Usaco2005]Part Acquisition(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=1674 想法很简单...将每一种看做一个点,如果i可以换成j,那么连边到j.. 费用都为1.. 然后拥 ...
- usaco silver
大神们都在刷usaco,我也来水一水 1606: [Usaco2008 Dec]Hay For Sale 购买干草 裸背包 1607: [Usaco2008 Dec]Patting Heads 轻 ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ3392: [Usaco2005 Feb]Part Acquisition 交易
3392: [Usaco2005 Feb]Part Acquisition 交易 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 26 Solved: ...
- bzoj:3392: [Usaco2005 Feb]Part Acquisition 交易
Description 奶牛们接到了寻找一种新型挤奶机的任务,为此它们准备依次经过N(1≤N≤50000)颗行星,在行星上进行交易.为了方便,奶牛们已经给可能出现的K(1≤K≤1000)种货物 ...
随机推荐
- DotProject首页、文档和下载 - 项目管理工具 - 开源中国社区
DotProject首页.文档和下载 - 项目管理工具 - 开源中国社区
- 【转】c++内存管理学习纲要
http://blog.csdn.net/zhanghefu/article/details/5003407 转自:http://blog.csdn.net/wdzxl198/article/deta ...
- 【c++】拷贝控制具体分析
我们可以定义拷贝操作,使类的行为看起来像一个值或者像一个指针,这取决于如何拷贝指针成员. 当我们拷贝一个像值的对象时,副本和原对象是完全独立的,改变副本不会对原对象有任何影响,反之亦然.标准库容器和s ...
- HTML大文件上传(博客迁移)
Html大文件上传:跳转 通过github和hexo进行搭建博客,主要是在没有网络的时候,可以本地访问,并支持markdown语法. 新博客地址:跳转
- day5-WordCount
1. wordcount示例开发 1.1. wordcount程序整体运行流程示意图 map阶段: 将每一行文本数据变成<单词,1>这样的kv数据 reduce阶段:将相同单词的一组k ...
- 使用POI操作Excel时new XSSFWorkbook ()报错java.lang.NoSuchMethodError解决方式
使用最新的POI3.11时,在执行 Workbook workBook = new XSSFWorkbook ();这段代码时出现错误: java.lang.NoSuchMethodError: j ...
- Win8系统如何在桌面行显示我的电脑
1 桌面右击-个性化 2 更改桌面图标-然后可以在桌面上显示需要的东西
- axis2开发webservice之编写Axis2模块(Module)
axis2中的模块化开发.能够让开发者自由的加入自己所需的模块.提高开发效率,减少开发的难度. Axis2能够通过模块(Module)进行扩展. Axis2模块至少须要有两个类,这两个类分别实现了Mo ...
- A7139 无线通信驱动(STM32) 添加FIFO扩展模式,能够发送超大数据包
A7139 拥有电磁波唤醒以及10mW的发射功率,很easy实现长距离通信,眼下測试有障碍物能够轻松达到300m以上. 通过几天的调试,眼下能够发送随意大小的数据包,大小为1-16KB.所有使用中断收 ...
- 嵌入式开发之davinci--- 8148 中dsp在dsp_drv.c中的processdata()加算法出现下边缘条纹问题
(1)问题原因 dsp在alglink_priv.c中做灰度处理发现,下面出现条纹,后面发现是cache 缓存没及时写进内存问题 (2)解决办法 for(frameId=0; frameId<f ...