http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1456

题意:
求最短路并且输出字典序最小的答案。

思路:
如果用dijkstra来做的话,会比较麻烦,这里直接用floyd会简单的多,只需要记录好后继路径即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; int n, m; int val[maxn];
int mp[maxn][maxn];
int path[maxn][maxn]; void floyd()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(mp[i][k]==INF || mp[k][j]==INF) continue;
int sum=mp[i][k]+mp[k][j]+val[k];
if(mp[i][j]>sum)
{
mp[i][j]=sum;
path[i][j]=path[i][k];
}
else if(mp[i][j]==sum && path[i][j]>path[i][k])
path[i][j]=path[i][k];
}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n) && n)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==-) mp[i][j]=INF;
path[i][j]=j; //i的后继路径为j
}
for(int i=;i<=n;i++) scanf("%d",&val[i]);
floyd(); while(true)
{
int s,t;
scanf("%d%d",&s,&t);
if(s==- && t==-) break;
printf("From %d to %d :\n",s,t);
printf("Path: %d",s);
int tmp=s;
while(tmp!=t)
{
printf("-->%d",path[tmp][t]);
tmp=path[tmp][t];
}
printf("\n");
printf("Total cost : %d\n\n",mp[s][t]);
}
}
return ;
}

ZOJ 1456 Minimum Transport Cost(floyd+后继路径记录)的更多相关文章

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

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

  2. 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)

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

  4. Minimum Transport Cost Floyd 输出最短路

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

  5. 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 ...

  6. HDU1385 Minimum Transport Cost (Floyd)

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

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

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

  8. NSOJ Minimum Transport Cost

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

  9. HDU 1385 Minimum Transport Cost( Floyd + 记录路径 )

    链接:传送门 题意:有 n 个城市,从城市 i 到城市 j 需要话费 Aij ,当穿越城市 i 的时候还需要话费额外的 Bi ( 起点终点两个城市不算穿越 ),给出 n × n 大小的城市关系图,-1 ...

随机推荐

  1. java注解使用

    1:定义注解 package chapter20.one; import java.lang.annotation.ElementType; import java.lang.annotation.R ...

  2. myeclipse maven工程调试调试

    1:使用了maven带的tomca插件进行启动.下面讲一下如何进行调试程序 2:在程序中打断点 3:选择>debug>debug configuration 在goals处添加启动命令 4 ...

  3. Ubuntu单用户模式(安全模式)

           说下我遇到的情况,ubuntu服务器,防火墙关闭,连的外网.服务器中毒,病毒自动生成用户,然后病毒进程开启启动,进程启动后,cpu立马占满,服务器立马卡死,本想着服务器启动后通过top命 ...

  4. FPKM\RPKM\TPM学习[转载]

    转自:http://www.360doc.com/content/18/0112/02/50153987_721216719.shtml 1.问题提出 在RNA-Seq的分析中,对基因或转录本的rea ...

  5. PAT Battle Over Cities [未作]

    1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highw ...

  6. [LeetCode] 257. Binary Tree Paths_ Easy tag: DFS

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  7. iOS 自定义日志输出

    在做iOS开发过程中,我们经常需要输出日志来查看某些数据是否打印出来,或者查看查个类是否被调用了. 系统默认的是NSLog(@"xxxx %d",1) ,但如果该APP要发布到商店 ...

  8. JS中“==”和“===”的原理和区别

    1.为什么讨论这个问题? - 有个说法,尽量不用==,而使用===,是这样吗? 2.分析问题,原理是什么? 下面说说ECMAScript 5 language specification里的说明: 1 ...

  9. <Convolutional Neural Network for Paraphrase Identification>

    code:https://github.com/chantera/bicnn-mi Yin的这篇论文提出了一种叫Bi-CNN-MI的架构,其中Bi-CNN表示两个使用Siamese框架的CNN模型:M ...

  10. 让nodepad++编辑时链接能双击打开

    让nodepad++编辑时链接能双击打开,Notepad++自动把代码或编辑状态里的链接或URL地址转成可点击的链接,当你双击该URL会打开相应的网页地址,不用复制地址到浏览器打开了,非常方便好用. ...