Part Acquisition(spfa输出路径)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4080 | Accepted: 1742 | Special Judge |
Description
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
* 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
* 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输出路径)的更多相关文章
- hdu Minimum Transport Cost(按字典序输出路径)
http://acm.hdu.edu.cn/showproblem.php? pid=1385 求最短路.要求输出字典序最小的路径. spfa:拿一个pre[]记录前驱,不同的是在松弛的时候.要考虑和 ...
- VS 工程的 输出路径和工作路径的区别
输出路径,是vs编译项目生成可执行文件的路径:工作路径是环境变量,比如我们在程序中写相对路径,就是以这个路径为基础的.在默认情况下,输出路径和工作路径都不写的话,默认是程序的bin下面的debug或者 ...
- HD1385Minimum Transport Cost(Floyd + 输出路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- C++builder XE 安装控件 及输出路径
C++builder XE 安装控件 与cb6不一样了,和delphi可以共用一个包. 启动RAD Studio.打开包文件. Project>Options>Delphi Compile ...
- HDU 1385 Minimum Transport Cost (最短路,并输出路径)
题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...
- web项目Log4j日志输出路径配置问题
问题描述:一个web项目想在一个tomcat下运行多个实例(通过修改war包名称的实现),然后每个实例都将日志输出到tomcat的logs目录下实例名命名的文件夹下进行区分查看每个实例日志,要求通过尽 ...
- VJP1071新年趣事之打牌(背包+输出路径)
简单的01背包 保存下方案总数 其实就是dp[v]值 输出路径dfs一下 #include <iostream> #include<cstdio> #include<cs ...
- (poj)3414 Pots (输出路径的广搜)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- Cmake 脚本对项目输出路径和输出头文件的路径定义
对Lib项目的统一输出路径以下时解决方案: set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Lib)set(CMAKE_LIBRARY_O ...
随机推荐
- 【转】sqlserver数据库之间的表的复制
以下以数据库t1和test为例. 1.复制表结构及资料 select * into 数据库名.dbo.表名 from 源表(全部数据) 如:select * into t1.dbo.YS1 ...
- CentOS下成功挂载xxxxxDVDx.iso并使用yum安装软件
CentOS下成功挂载xxxxxDVDx.iso并使用yum安装软件 **不断尝试,终能到达彼岸** 测试环境为Win7 32位,VirtualBOx4.2.16+CentOS6.5,可分别到virt ...
- Ubuntu 14.10 编译 qt4.8.6
0. 假设你已经可以在上面写基本的C++程序.(即:c/C++开发环境已经就绪)1. $ sudo apt-get build-dep libqt4-dev 2. $ sudo apt-get ins ...
- android-JSON解析
构建JSON文本 方法1. // 假设现在要创建这样一个json文本 // { // "phone" : ["12345678", "87654321 ...
- 批量删除Kindle library 中的不想要的书籍
这是一个书签形式的脚本 有全选 批量删除什么的 亚马逊本身的删除太麻烦了 网上转的 原帖在这里http://tieba.baidu.com/p/2249582757 改进版本http://t ...
- Loggerly技术架构
https://www.loggly.com/blog/topic/log-management-technology/
- 数据结构之后缀数组suffix array
在字符串处理当中,后缀树和后缀数组都是非常有力的工具,其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料.其实后缀是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现,能够实现后缀树的很多 ...
- jQuery.merge 源码阅读
jQuery.merge(first,second) 概述 合并两个数组 返回的结果会修改第一个数组的内容——第一个数组的元素后面跟着第二个数组的元素. 参数 first:第一个待处理数组,会改变其中 ...
- vs linq to db template
linq to db template 支持sqlite. mysql .db2. accress. oracle. Firebird等多种数据库以linq方式操作数据. NuGet NuGet 是 ...
- projecteuler---->problem=8----Largest product in a series
title: The four adjacent digits in the 1000-digit number that have the greatest product are 9 9 8 9 ...