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. linux核心之进程管理

    进程就是处于执行期的程序(目标码存放在某中介质上).进程并不仅仅局限于一段可执行程序代码,通常还包括其他资源,例如打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内存映射的内存地址空间 ...

  2. Java关键字static

    链接地址:http://www.cnblogs.com/devinzhang/archive/2011/12/13/2286367.html static表示“全局”或者“静态”的意思,用来修饰成员变 ...

  3. What I Have Lived For(我为什么而活着-罗素)

    What I Have Lived For by Bertrand Russell Three passions, simple but overwhelmingly strong, have gov ...

  4. 关于playframework2.5

    加入了很多新东西: 1.用akka streams 替换了大部分 iteratee-based async io,当然还有一些模块在用iteratees 2.java 的一些API 做了调整升级,以及 ...

  5. [LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree

    题目来源: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意分析: 给出一个排好序的数组,根据这 ...

  6. [LeetCode]题解(python):005-Longest Palindromic Substring

    题目来源: https://leetcode.com/problems/longest-palindromic-substring/ 题意分析: 这道题目是输入一段不超过1000的字符串,输出最长的回 ...

  7. python自学笔记(五)python文本操作

    一.python自带方法 r:read 读 w:write 写 a:append 尾行追加 先命令行进入python后 >>>d = open('a.txt','w') #在对应路径 ...

  8. 远程管理服务SSHD

    安装SSH yum install openssh

  9. Java并发编程总结2——慎用CAS(转)

    一.CAS和synchronized适用场景 1.对于资源竞争较少的情况,使用synchronized同步锁进行线程阻塞和唤醒切换以及用户态内核态间的切换操作额外浪费消耗cpu资源:而CAS基于硬件实 ...

  10. [HDU 3336]Count the String[kmp][DP]

    题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...