最短路的路径打印问题 同时路径要是最小字典序

字典序用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(路径打印)的更多相关文章

  1. Minimum Transport Cost(floyd+二维数组记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  2. HDU1385 Minimum Transport Cost (Floyd)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  3. hdu 1385 Minimum Transport Cost(floyd &amp;&amp; 记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. HDU 1385 Minimum Transport Cost (Dijstra 最短路)

    Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...

  5. hdu 1385 Minimum Transport Cost (Floyd)

    Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  6. ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)

    题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...

  7. NSOJ Minimum Transport Cost

    These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...

  8. Minimum Transport Cost Floyd 输出最短路

    These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...

  9. 【floyd存字典序路径】【HDU1385】【Minimum Transport Cost】

    题目大意 求多组i到j的最短路径 并输出字典序最小.... 现在只会floyd的方式 利用dis[i][j] 表示i到j的路径中i 后面的节点 更新是比较dis[i][j] dis[i][k]. 记住 ...

随机推荐

  1. getattr getattribute setattr hasattr delattr

    getattr是返回对象属性value的函数,用法:getattr(object,attribute_name[,default]) 如果对象拥有属性,则返回属性value,如果对象没有该属性并且也没 ...

  2. Eclipse文件路径

    经常我们需要读取某个文件,一般情况下,在Eclipse工程中,路径为./src/....

  3. mysql一次查询,返回多个统计结果

    1.sum(if) select sum(if(status=1,1,0)) as s1_count,sum(if(status=2,1,0)) as s2_countfrom order; 2.co ...

  4. Windows和Mac上NodeJS和Express的安装

    一.安装NodeJS,官网上下载,https://nodejs.org/en/ 直接下一步安装就行了. 打开命令行工具,输入 node -v 则会出现node的版本,则成功了. 下面我们介绍如何安装e ...

  5. 【vim】跳转到上/下一个修改的位置

    当你编辑一个很大的文件时,经常要做的事是在某处进行修改,然后跳到另外一处.如果你想跳回之前修改的地方,使用命令: Ctrl+o 来回到之前修改的地方 类似的: Ctrl+i 会回退上面的跳动.

  6. 利用capability特征加强Linux系统安全【转】

    转自:https://blog.csdn.net/fivedragon/article/details/676849 1.简介 UNIX是一种安全操作系统,它给普通用户尽可能低的权限,而把全部的系统权 ...

  7. phantomjs 截取twitter的网页(动态生成的页面)

    // This example shows how to render pages that perform AJAX calls// upon page load.//// Instead of w ...

  8. weblogic实时监控开发

    参考api文档 https://docs.oracle.com/cd/E13222_01/wls/docs90/wlsmbeanref/core/index.html https://docs.ora ...

  9. C++:vector中的v.at(0)和v[0]的区别

    设v是一个vector的对象, 如果v是非空的,则v.at(0)和v[0]是没有区别的,都是取数组中第一个值: 如果v是空的,则v.at(0)会抛出异常(exception std::out_of_r ...

  10. 分布式调用技术 RPC VS REST

    一 分布式调用大体上就分为两类,RPC式的,REST式的,两者的区别主要是就是: 1. RPC是面向动作的(方法调用) 2. REST是面向资源的(URL表示资源,HTTP动词表示动作) 从变现形式来 ...