题目链接:poj1122 FDNY to the Rescue!

题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给出火警位置所在的交叉路口 和 一个或多个消防站所处的交叉路口位置。输出要求按消防站到火警位置所需时间从小到大排列,输出信息包括消防站位置(初始位置),火警位置(目标位置),所需时间,最短路径上每个交叉路口。

题解:反向建图,从火警位置求一次最短路,求最短路时记录路径,按时间从小到大输出。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #define CLR(a,b) memset((a),(b),sizeof((a)))
  5. using namespace std;
  6.  
  7. const int inf = 0x3f3f3f3f;
  8. const int N = ;
  9. int n, fire;
  10. int g[N][N];//邻接矩阵存图
  11. int s[N], d[N];
  12. int path[N];//表示v0到vi最短路径顶点vi的前一个顶点序号
  13. int shortest[N];//存储最短路径上的各个顶点序号
  14. struct node{
  15. int u, v, t;
  16. }a[N];
  17. int cmp(node a, node b){
  18. return a.t < b.t;
  19. }
  20. void dij(){
  21. int i, j, k;
  22. CLR(s, );
  23. for(i = ; i <= n; ++i){
  24. d[i] = g[fire][i];
  25. if(i != fire)
  26. path[i] = fire;
  27. else
  28. path[i] = -;
  29. }
  30. s[fire] = ;
  31. d[fire] = ;
  32. for(i = ; i < n-; ++i){
  33. int mi = inf, u = ;
  34. for(j = ; j <= n ; ++j){
  35. if(!s[j] && d[j] < mi){
  36. u = j; mi = d[j];
  37. }
  38. }
  39. s[u] = ;
  40. for(k = ; k <= n; ++k){
  41. if(!s[k] && d[u] + g[u][k] < d[k]){
  42. d[k] = d[u] + g[u][k];
  43. path[k] = u;
  44. }
  45. }
  46. }
  47. }
  48. int main(){
  49. int i, j, x, cnt;
  50. scanf("%d", &n);
  51. for(i = ; i <= n; ++i){//边反向存储
  52. for(j = ; j <= n; ++j){
  53. scanf("%d", &x);
  54. if(x == -)
  55. g[j][i] = inf;
  56. else
  57. g[j][i] = x;
  58. }
  59. }
  60. scanf("%d", &fire);
  61. dij();
  62. cnt = ;
  63. while(scanf("%d", &x) == ){
  64. a[cnt].u = x;
  65. a[cnt].v = fire;
  66. a[cnt++].t = d[x];
  67. }
  68. sort(a, a+cnt, cmp);
  69. printf("Org\tDest\tTime\tPath\n");
  70. for(i = ; i < cnt; ++i){
  71. printf("%d\t%d\t%d", a[i].u, a[i].v, a[i].t);
  72. CLR(shortest, );
  73. int k = ;//表示shortest数组中最后一个元素的下标
  74. shortest[k] = a[i].u;
  75. while(path[shortest[k]] != -){
  76. k++;
  77. shortest[k] = path[shortest[k-]];
  78. }
  79. for(j = ; j <= k; ++j) //倒向追踪,但是反向建图,因此正向输出
  80. printf("\t%d", shortest[j]);
  81. puts("");
  82. }
  83. return ;
  84. }

poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)的更多相关文章

  1. POJ-1122 FDNY to the Rescue!---Dijkstra+反向建图

    题目链接: https://vjudge.net/problem/POJ-1122 题目大意: 给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路 ...

  2. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  3. POJ3687——Labeling Balls(反向建图+拓扑排序)

    Labeling Balls DescriptionWindy has N balls of distinct weights from 1 unit to N units. Now he tries ...

  4. HDU 2647 Reward 【拓扑排序反向建图+队列】

    题目 Reward Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to d ...

  5. HUD2647 Reward_反向建图拓扑排序

    HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...

  6. HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...

  7. POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    Silver Cow Party Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Su ...

  8. HPU 3639--Hawk-and-Chicken【SCC缩点反向建图 &amp;&amp; 求传递的最大值】

    Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. CodeForces - 350B(反向建图,)

    B - Resort CodeForces - 350B B. Resort time limit per test 2 seconds memory limit per test 256 megab ...

随机推荐

  1. 安装64位版Oracle11gR2后无法启动SQLDeveloper的解决方案

    安装64位版Oracle11gR2后发现启动SQL Developer时弹出配置java.exe的路径,找到Oracle自带java.exe后产生的路径“C:\app\用户名\product\11.2 ...

  2. [51NOD1537] 分解(递推,矩阵快速幂)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1537 思路:一开始用二项式拆了一下,发现这个式子的形式总能变成 ...

  3. JavaScript Replace 多个字符

    <html> <head> <title></title> <script language="javascript"> ...

  4. FJNU 1152 Fat Brother And Integer(胖哥与整数)

    FJNU 1152 Fat Brother And Integer(胖哥与整数) Time Limit: 1000MS   Memory Limit: 257792K [Description] [题 ...

  5. 1.mybatis简介

    mybatis简介 MyBatis 是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为My ...

  6. elcipse 中利用maven创建web工程

    如何创建: http://huxiaoheihei.iteye.com/blog/1766986 遇到的问题: 1: 如果spring MVC配置了 <servlet> <servl ...

  7. Nginx基础知识————生成自签名ca 证书 使nginx 支持https

    创建服务器私钥,命令会让你输入一个口令: $ openssl genrsa -des3 -out server.key 1024 创建签名请求的证书(CSR): $ openssl req -new ...

  8. maven常见问题问答

    1.前言 Maven,发音是[`meivin],"专家"的意思.它是一个很好的项目管理工具,很早就进入了我的必备工具行列,但是这次为了把project1项目完全迁移并应用maven ...

  9. 如何查看与刷新DNS本地缓存

    如何查看与刷新DNS本地缓存 一.查看DNS本地缓存 在cmd窗口输入:ipconfig/displaydns 二.刷新DNS本地缓存 在cmd窗口输入:ipconfig/flushdns 之后输入: ...

  10. Android LayoutInflater 动态地添加删除View

    我想实现点击一个按钮(或其他的事件)添加或删除View,网上找到了LayoutInflater这个类. 下面是我自己一些经验: android官网上LayoutInflater的API:http:// ...