Minimum Transport Cost HDU1385(路径打印)
最短路的路径打印问题 同时路径要是最小字典序
字典序用floyd方便很多
学会了两种打印路径的方法!!!
#include <stdio.h>
#include <string.h>
#define N 110
#define INF 1000000000
int d[N][N],path[N][N],c[N];
int n,cost;
int s,t; void input()
{
int i,j,w;
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
scanf("%d",&d[i][j]);
if(d[i][j]==-) d[i][j]=INF;
path[i][j]=j;
}
for(i=; i<=n; i++)
scanf("%d",&c[i]); return ;
} void Floy()
{
int i,j,k; for(k=; k<=n; k++) //中转站k
for(i=; i<=n; i++) //起点和终点i,j
for(j=; j<=n; j++)
{
if( d[i][j] > d[i][k]+d[k][j]+c[k] )
{
d[i][j]=d[i][k]+d[k][j]+c[k];
path[i][j]=path[i][k];
} else if( d[i][j] == d[i][k]+d[k][j]+c[k] )
{
if( path[i][j] > path[i][k])
{
d[i][j]=d[i][k]+d[k][j]+c[k];
path[i][j]=path[i][k];
}
} }
return ;
} void print_path(int u , int v) //u是起点v是终点
{
int k;
if(u==v)
{
printf("%d",v);
return ;
}
k=path[u][v];
printf("%d-->",u);
print_path(k,v);
}
int main()
{
while(scanf("%d",&n)!=EOF && n)
{
input();
Floy(); while(scanf("%d%d",&s,&t))
{
if( s==- && t==-) break;
cost=d[s][t];
if(s==t) //起点和终点相同
{
printf("From %d to %d :\n",s,t);
printf("Path: %d\n",s);
printf("Total cost : %d\n\n",cost);
continue;
}
printf("From %d to %d :\n",s,t);
printf("Path: ");
print_path(s,t);
printf("\n");
printf("Total cost : %d\n\n",cost);
}
}
return ;
}
dijkstra实现
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=;
int pos=;
int G[maxn][maxn],d[maxn],c[maxn],pre[maxn];
bool vis[maxn];
int n,st,ed;
void dfs(int u,char *s)
{
if(u==st) return ;
dfs(pre[u],s);
s[pos++]=u+'';
}
bool cmp(int u,int v)
{
char s1[maxn],s2[maxn];
pos=;
dfs(u,s1);
s1[pos]='\0';
pos=;
dfs(v,s2);
s2[pos]='\0';
if(strcmp(s1,s2)==) return true;
return false;
}
void dijkstra(int st)
{
fill(d,d+maxn,inf);
for(int i=;i<=n;i++) pre[i]=i;
d[st]=;
for(int i=;i<=n;i++)
{
int u=-,MIN=inf;
for(int j=;j<=n;j++)
{
if(!vis[j]&&d[j]<MIN)
{
MIN=d[j];
u=j;
}
}
if(u==-) return ;
vis[u]=true;
for(int v=;v<=n;v++)
{
if(G[u][v]!=inf&&vis[v]==false&&G[u][v]+d[u]+c[u]<d[v])
{
d[v]=G[u][v]+d[u]+c[u];
pre[v]=u;
}
else
{
if(G[u][v]+d[u]+c[u]==d[v]&&cmp(v,u))
pre[v]=u;
}
} }
} void show(int v)
{
if(v==st)
{
cout<<v;
return ;
}
show(pre[v]);
cout<<"-->"<<v;
}
int main()
{
while (cin>>n&&n!=) {
memset(vis,false,sizeof(vis));
//fill(G[0],G[0]+maxn*maxn,inf);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>G[i][j];
if(G[i][j]==-)
G[i][j]=inf;
}
}
for(int i=;i<=n;i++)
cin>>c[i];
while (cin>>st>>ed)
{
memset(vis,false,sizeof(vis));
if(st==-&&ed==-) break;
if(st==ed) {
printf("From %d to %d :\n",st,ed);
printf("Path: %d\n",st);
printf("Total cost : 0\n\n"); }
else{
dijkstra(st);
printf("From %d to %d :\n",st,ed);
printf("Path: ");
show(ed);
cout<<endl;
printf("Total cost : %d\n",d[ed]-c[st]);
cout<<endl;
}
}
}
}
Minimum Transport Cost HDU1385(路径打印)的更多相关文章
- Minimum Transport Cost(floyd+二维数组记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- HDU1385 Minimum Transport Cost (Floyd)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu 1385 Minimum Transport Cost(floyd && 记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
- hdu 1385 Minimum Transport Cost (Floyd)
Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- NSOJ Minimum Transport Cost
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- Minimum Transport Cost Floyd 输出最短路
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- 【floyd存字典序路径】【HDU1385】【Minimum Transport Cost】
题目大意 求多组i到j的最短路径 并输出字典序最小.... 现在只会floyd的方式 利用dis[i][j] 表示i到j的路径中i 后面的节点 更新是比较dis[i][j] dis[i][k]. 记住 ...
随机推荐
- [C++]埃拉托色尼算法
/* 埃拉托色尼算法 问题描述:定义一个正整数n,求0-n范围以内的所有质数 @date 2017-03-06 @author Johnny Zen */ #include<iost ...
- python Twisted安装报错
系统 mac pro 错误信息: IOError: [Errno 63] File name too long: '/var/folders/72/byjy11cs0dj_z3rjtxnj_nn000 ...
- java Comparable 和 Comparator接口区别
Comparable 简介 Comparable 是排序接口. 若一个类实现了Comparable接口,就意味着“该类支持排序”. 即然实现Comparable接口的类支持排序,假设现在存在“实现C ...
- OLAP和OLTP的区别(基础知识) 【转】
联机分析处理 (OLAP) 的概念最早是由关系数据库之父E.F.Codd于1993年提出的,他同时提出了关于OLAP的12条准则.OLAP的提出引起了很大的反响,OLAP作为一类产品同联机事务处理 ( ...
- n个随机变量中第k小值的期望
Problem 有\(n\)个相互独立的取值在\([0,1]\)的随机变量,问其中第\(k\)小的变量期望是多少? Solution 之前pkuwc的神仙找我换友链,让我受宠若惊.. 我今天突然翻到他 ...
- 【转】C Runtime Library的来历
由于我看到的文章也是转载且未提供原文链接,所以这里没有提供原文链接! msvcrt.dll(名称:Microsoft C Runtime Library)提供了printf,malloc,strcpy ...
- MPI 在Windows10 上安装,使用VS2013编译生成可执行程序
原文地址:http://www.cnblogs.com/leijin0211/p/6851789.html 参考博客: http://www.cnblogs.com/shixiangwan/p/662 ...
- Linux无权限上传文件解决办法
无权限上传文件解决办法 1.当前登录的普通用户:user1/password1 2.切换到管理员(user2)用户: sudo su - user2 输入user2用户的密码:password2 或者 ...
- [USACO12DEC]逃跑的BarnRunning Away From…
题意 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个 题解 似乎有好多种做法啊……然而蒟蒻只会打打主席树的板子…… 调了一个上午一直WA……狠下心来重打一遍居然直接一遍过 ...
- transfer pdf to png
#! /bin/bash # # transfer pdf to png if [ $# != 1 ] ; then echo "USAGE: $0 PDF FILE ABSOLUTELY ...