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]. 记住 ...
随机推荐
- Git Gui基本使用情况
本教程将讲述:gitk的Git Gui的部分常用功能和使用方法,包括:建库.克隆(clone).上传(push).下载(pull - fetch).合并(pull - merge). ———————— ...
- js 布局转换问题
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- WF控制台工作流(1)
简单使用WF工作流示例: using System; using System.Linq; using System.Activities; using System.Activities.State ...
- luogu P1445 [Violet]嘤F♂A
博主决定更博文啦 这道题一开始没什么思路啊qwq 要求 \(\frac{1}{x}+\frac{1}{y}=\frac{1}{n!}\) 的正整数解总数 首先通分,得 \[\frac{x+y}{xy} ...
- C#中富文本编辑器Simditor带图片上传的全部过程(MVC架构的项目)
描述:最近c#项目中使用富文本编辑器Simditor,记录一下以便以后查看. 注:此项目是MVC架构的. 1.引用文件 项目中引用相应的css和js文件,注意顺序不能打乱,否则富文本编辑器不会正常显示 ...
- 阿里云apache服务器外网无法访问(配置安全组,添加80服务)
CentOS的系统 ,已经安装好了 apache php mysql 常规排错过程(ps:没耐心的童鞋请直接看最后一步,学习在阿里云控制台配置 安全组,允许 http服务) 第一步:检查apache ...
- SRS服务器搭建,ffmpeg 本地推流,srs从本地拉流
参考: https://github.com/ossrs/srs/wiki/v2_CN_SampleFFMPEG git clone https://github.com/ossrs/srs cd s ...
- Jade教程
Jade 是一个高性能的模板引擎,它深受 Haml 影响,它是用 JavaScript 实现的,并且可以供 Node 使用. 如何在jade模板上加业务逻辑 if res.length==5 h1= ...
- OpenCV:Debug和Release模式 && 静态和动态编译
1.Release和Debug的区别 Release版称为发行版,Debug版称为调试版. Debug中可以单步执行.跟踪等功能,但生成的可执行文件比较大,代码运行速度较慢.Release版运行速度较 ...
- Git入门——远程仓库及分支管理
关于本地版本库的操作,请见:Git入门--本地版本库操作 本篇提到的所有命令: 小结 前面提到,Git相对于传统的SVN有着很大的优势,其中之一就在于集中式系统中,版本库只能存在于中央服务器上:而在G ...