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]. 记住 ...
随机推荐
- fcn16s
- PWA,SPA,MPA
PWA渐进式应用 特点: 不会部署到应用商店. 离线应用,通过设备进行存储规划 在发布了pwa的网站,浏览器会询问是否安装app到主屏. 方便分享,通过url. 可推送通知 . 通过service w ...
- Shiro+Spring+SpringMVC+Mybatis整合
Demo源码地址,v1.0分支 https://github.com/jxjy/hr
- http://blog.csdn.net/w_e_i_/article/details/70766035
http://blog.csdn.net/w_e_i_/article/details/70766035
- A - 地精部落 (DP)
题目链接:https://cn.vjudge.net/contest/281960#problem/A 题目大意:中文题目. 具体思路:首先,如果有一段是山谷的话,那么这一段中也能用来表示山峰,只要将 ...
- Maven继承
继承为了消除重复,可以把pom 中很多相同的配置提取出来:如:grouptId, version 等. 在使用的时候子工程直接继承父工程的依赖版本号,子工程中不再需要指定具体版本号,方便统一管控项目的 ...
- linux笔记_day03
1.命令行展开{} mkdir -p a/b/{c,d/e} 2.-v verbose 详细的 3.touch touch - change file timestamps 4.stat 文件 显示 ...
- C语言中malloc函数返回值是否需要类型强制转换问题
1. 在C语言中, 如果调用的函数没有函数原型, 则其返回值将默认为 int 型. 考虑调用malloc函数时忘记了 #include <stdlib.h>的情况 此时malloc函数返回 ...
- 【逆向工具】IDA使用5-( string、图形化与视图的切换、图形化显示反汇编地址、自动注释、标签使用)
分析petya病毒时新学会的技巧. IDA技巧1 : string 提取文件中的字符串内容,如果看到一些文件字符串可以定位到关键的函数中. view -> open subview -> ...
- Linux下clock计时函数学习
平时在Linux和Winows下都有编码的时候,移植代码的时候免不了发现一些问题.1. 你到底准不准?关于clock()计时函数首先是一段简单的测试代码,功能为测试从文本文件读取数据并赋值给向量最后打 ...