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. Use Excel to write insert SqlScript

    ="insert into Process.dbo.TempHampInGoodStandingLoanList (Loan_No,ProgramType,ModificationEffec ...

  2. 更改mysql 数据库名称

    //创建新数据库 CREATE DATABASE hbwebTemporary; //移植每个表 RENAME TABLE hbweb.aircraft_info TO hbwebTemporary. ...

  3. JAVA处理XML

    <xml>  <ToUserName><![CDATA[toUser]]></ToUserName>  <FromUserName>< ...

  4. Java 网络编程(五) 使用TCP/IP的套接字(Socket)进行通信

    链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/10/2952616.html 使用TCP/IP的套接字(Socket)进行通信 套接字Socke ...

  5. Mac环境下Myeclispe2015工具的安装与破解

    链接地址:http://www.07net01.com/2015/08/919753.html 07net01.com 发布于 2015-08-30 22:19:37 分类:IT技术 阅读(306) ...

  6. JS学习之事件冒泡

    (1)什么是事件起泡      首先你要明白一点,当一个事件发生的时候,该事件总是有一个事件源,即引发这个事件的对象,一个事件不能凭空产生,这就是事件的发生. 当事件发生后,这个事件就要开始传播.为什 ...

  7. php中0,空,null和false的区别

    <? $str1 = null; $str2 = false; echo $str1==$str2 ? ‘相等’ : ‘不相等’; $str3 = ""; $str4 = 0 ...

  8. Tomcat 启动 Debug模式

    如果debug启动遇到如下错误: ERROR: transport error 202: gethostbyname: unknown host ERROR: JDWP Transport dt_so ...

  9. css布局: 两栏 自适应高度

    只使用css实现 有两种方式, 一种是通过相对定位,再绝对定位获取父亲元素的高度, 一种是通过margin-bottom:-999em;padding-bottom: 999em; 父亲元素超出隐藏 ...

  10. 【转】关于UItableViewCell的accessoryType属性

    转载自:http://blog.csdn.net/kmyhy/article/details/6442351 使用的话,例如: cell.accessoryType = UITableViewCell ...