Part Acquisition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4080   Accepted: 1742   Special Judge

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).

* Lines 2..T+1: The ordered list of the objects that the cows possess in the sequence of trades.

Sample Input

6 5
1 3
3 2
2 3
3 1
2 5
5 4

Sample Output

4
1
3
2
5
题意;母牛想用草换k星奶机;输出路径;bellman只是单纯的找dis的最小值,会陷入2 3,3 2循环
spfa:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=1010;
const int MAXM=50010;
int vis[MAXN],dis[MAXN],pre[MAXN];
int head[MAXM];
queue<int>S;
int n,k,edgnum;
struct Edge{
int from,to,next;
}edg[MAXM];
void initial(){
mem(head,-1);
edgnum=0;
mem(pre,0);
}
void add(int u,int v){
Edge E={u,v,head[u]};
edg[edgnum]=E;
head[u]=edgnum++;
}
void print(int x){
if(x==0)return;
print(pre[x]);
printf("%d\n",x);
}
void spfa(int sx){
while(!S.empty())S.pop();
mem(vis,0);mem(dis,INF);
S.push(sx);vis[sx]=1;dis[sx]=1;
while(!S.empty()){
int u=S.front();
vis[u]=0;
S.pop();
for(int i=head[u];i!=-1;i=edg[i].next){
int v=edg[i].to;
if(dis[v]>dis[u]+1){
dis[v]=dis[u]+1;
if(!vis[v]){
pre[v]=u;
vis[v]=1;
S.push(v);
}
}
}
}
if(dis[k]==INF){
puts("-1");
return;
}
printf("%d\n",dis[k]);
print(k);
return;
}
int main(){
while(~scanf("%d%d",&n,&k)){
initial();
int i,j,u,v;
F(i,n){
scanf("%d%d",&u,&v);
add(u,v);
}
spfa(1);
}
return 0;
}

bellman死循环:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=50010;
struct Node{
int u,v;
}dt[MAXN<<1];
int dis[MAXN],pre[MAXN];
int edgnum;
int n,k;
void add(int u,int v){
dt[edgnum].u=u;
dt[edgnum++].v=v;
}
void print(int x){
if(pre[x]==0)return;
print(pre[x]);
printf("%d\n",x);
}
int Bellman(int u){
mem(dis,INF);dis[u]=0;
for(int i=1;i<=k;i++){
int u,v;
for(int j=0;j<edgnum;j++){
// dis[v]=min(dis[v],dis[u]+1);
if(dis[v]>dis[u]+1){
dis[v]=dis[u]+1;
pre[v]=u;
}
}
}
if(dis[k]==INF)return -1;
print(k);
return dis[k];
}
int main(){
while(~scanf("%d%d",&n,&k)){
int i,j;
edgnum=0;
mem(pre,0);
F(i,n){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
printf("%d\n",Bellman(1));
}
return 0;
}

dijkscra;不能输出路径。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=1010;
const int MAXM=50010;
int vis[MAXN],dis[MAXN],pre[MAXN];
int mp[MAXN][MAXN];
int n,k,edgnum;
void print(int x){
if(x==0)return;
print(pre[x]);
printf("%d\n",x);
}
void dijkscra(int sx){
mem(dis,INF);mem(vis,0);
dis[sx]=1;//vis[sx]=1;
while(true){
int t=-1,i;
F(i,n){
if(!vis[i]&&(t==-1||dis[i]<dis[t]))t=i;
}
if(t==-1)break;
vis[t]=1;
F(i,n)dis[i]=min(dis[i],dis[t]+mp[t][i]);
}
if(dis[k]==INF){
puts("-1");return;
}
printf("%d\n",dis[k]);
//print(k);
}
int main(){
while(~scanf("%d%d",&n,&k)){
mem(mp,INF);mem(pre,0);
int i,j,u,v;
F(i,n){
scanf("%d%d",&u,&v);
mp[u][v]=1;
}
dijkscra(1);
}
return 0;
}

Part Acquisition(spfa输出路径)的更多相关文章

  1. hdu Minimum Transport Cost(按字典序输出路径)

    http://acm.hdu.edu.cn/showproblem.php? pid=1385 求最短路.要求输出字典序最小的路径. spfa:拿一个pre[]记录前驱,不同的是在松弛的时候.要考虑和 ...

  2. VS 工程的 输出路径和工作路径的区别

    输出路径,是vs编译项目生成可执行文件的路径:工作路径是环境变量,比如我们在程序中写相对路径,就是以这个路径为基础的.在默认情况下,输出路径和工作路径都不写的话,默认是程序的bin下面的debug或者 ...

  3. HD1385Minimum Transport Cost(Floyd + 输出路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. C++builder XE 安装控件 及输出路径

    C++builder XE 安装控件 与cb6不一样了,和delphi可以共用一个包. 启动RAD Studio.打开包文件. Project>Options>Delphi Compile ...

  5. HDU 1385 Minimum Transport Cost (最短路,并输出路径)

    题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...

  6. web项目Log4j日志输出路径配置问题

    问题描述:一个web项目想在一个tomcat下运行多个实例(通过修改war包名称的实现),然后每个实例都将日志输出到tomcat的logs目录下实例名命名的文件夹下进行区分查看每个实例日志,要求通过尽 ...

  7. VJP1071新年趣事之打牌(背包+输出路径)

    简单的01背包 保存下方案总数 其实就是dp[v]值 输出路径dfs一下 #include <iostream> #include<cstdio> #include<cs ...

  8. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. Cmake 脚本对项目输出路径和输出头文件的路径定义

    对Lib项目的统一输出路径以下时解决方案: set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Lib)set(CMAKE_LIBRARY_O ...

随机推荐

  1. c 转置字符串You are a so cheap man ->man cheap so a are You

    解题思路: 1.将字符串转置 2.对转置后的字符串中单词转置 #include<stdio.h> #include<string.h> #include<stdlib.h ...

  2. $timeout, $interval

    $timeout, $interval   layout: posttitle: Angular@1.4.3 中文 API 服务篇 $timeout & $intervaldesc: '$ti ...

  3. 未能加载文件或程序集“**, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项。试图加载格式不正确的程序。

    未能加载文件或程序集“Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项.试图加载格式不正确的程序. 原来, ...

  4. UltraISO做U盘启动盘教程

    用UltraISO做U盘启动盘教程 注意:制作前请先备份u盘内重要文件 vista.win7系统需要以管理员身份运行UltraISO,其他系统直接运行即可 1.打开UltraISO选择iso镜像文件 ...

  5. java的IO流包装不当导致从网页获取的数据出现乱码

    从网页上获取数据时必须要注意字符集的问题.处理不慎确实苦不堪言. 例如通过URL连接时,将字节流InputStream包装成字符流(以便直接存为String)时,一定要注意加上charsetName这 ...

  6. nrf51 官方PWM库

    地址:https://github.com/NordicSemiconductor/nrf51-pwm-library nrf_pwm_init函数 初始化PWM参数 设置输出pwm的gpio pin ...

  7. linux如何关闭selinux?

    首先我们可以用命令来查看selinux的状态getenforce 这个命令可以查看到selinux的状态,当前可以看到是关闭状态的.还有一个命令也可以查看出selinux的状态.sestatus -v ...

  8. linux 的 ping 原理

    ping命令的工作原理是: ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具. 他向网络上的另一个主机系统发送ICMP报文,如果指定系统得到了报文,它将把报文原样传回给发送者,这 ...

  9. Eclipse RCP扩展

    它跟普通的插件扩展点是一样的写法,你只需要看 eclipse 自带的源码,打开你的 eclipse 目录,插件 *.exsd 找到后在 eclipse 中查看这个文件夹对应的插件的源码,里面有 plu ...

  10. Google Play和基于Feature的过滤 —— Feature 参考手册

    翻译自 Features Reference 下表列出了软/硬件Feature和权限的参考信息,它们被用于GooglePlay. 硬件feature 下面列出了被大多数当前发布的平台所支持的硬件功能描 ...