题目链接:HDU - 1599

杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个景区。现在8600需要你帮他找一条这样的路线,并且花费越少越好。
Input
第一行是2个整数N和M(N <= 100, M <= 1000),代表景区的个数和道路的条数。
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <=
100)。
Output
对于每个测试实例,如果能找到这样一条路线的话,输出花费的最小值。如果找不到的话,输出"It's
impossible.".
 
题意描述:中文题,如上所述。
算法分析:用Floyd求解最小环的问题。有关最小环的定义和求解方法,大家可以去这个博客学习。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<algorithm>
  7. #define inf 0x7fffffff
  8. using namespace std;
  9. const int maxn=+;
  10.  
  11. int n,m;
  12. int dist[maxn][maxn],edge[maxn][maxn];
  13.  
  14. void floyd()
  15. {
  16. int ans=;
  17. for (int i= ;i<=n ;i++)
  18. {
  19. for (int j= ;j<=n ;j++)
  20. dist[i][j]=edge[i][j];
  21. }
  22. for (int k= ;k<=n ;k++)
  23. {
  24. for (int i= ;i<k ;i++)
  25. {
  26. for (int j=i+ ;j<k ;j++)
  27. {
  28. if (dist[j][i]+edge[i][k]+edge[k][j]<)
  29. ans=min(ans,dist[j][i]+edge[i][k]+edge[k][j]);
  30. }
  31. }
  32. for (int i= ;i<=n ;i++)
  33. {
  34. for (int j= ;j<=n ;j++)
  35. {
  36. if (dist[i][j]>dist[i][k]+dist[k][j])
  37. dist[i][j]=dist[i][k]+dist[k][j];
  38. }
  39. }
  40. }
  41. if (ans==) printf("It's impossible.\n");
  42. else printf("%d\n",ans);
  43. }
  44.  
  45. int main()
  46. {
  47. while (scanf("%d%d",&n,&m)!=EOF)
  48. {
  49. for (int i= ;i<=n ;i++)
  50. {
  51. for (int j= ;j<=n ;j++)
  52. edge[i][j]=;
  53. edge[i][i]=;
  54. }
  55. int x,y,cost;
  56. for (int i= ;i<m ;i++)
  57. {
  58. scanf("%d%d%d",&x,&y,&cost);
  59. if (edge[x][y]>cost) edge[x][y]=edge[y][x]=cost;
  60. }
  61. floyd();
  62. }
  63. return ;
  64. }

hdu 1599 find the mincost route 最小环的更多相关文章

  1. HDU 1599 find the mincost route(floyd求最小环 无向图)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  2. hdu 1599 find the mincost route (最小环与floyd算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 find the mincost route Time Limit: 1000/2000 MS ...

  3. hdu 1599 find the mincost route(无向图的最小环)

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. hdu 1599 find the mincost route floyd求无向图最小环

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu 1599 find the mincost route(flyod求最小环)

    Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1, ...

  6. HDU 1599 find the mincost route (无向图floyd最小环详解)

    转载请注明出处:http://blog.csdn.net/a1dark 分析:终于弄懂了floyd的原理.以前的理解一直肤浅.所以一做到floyd应用的题.就拙计了.其实floyd的本质DP.利用前K ...

  7. HDU 1599 find the mincost route (无向图的最小环)

    题意: 给一个带权无向图,求其至少有3个点组成的环的最小权之和. 思路: (1)DFS可以做,实现了确实可以,只是TLE了.量少的时候应该还是可以水一下的.主要思路就是,深搜过程如果当前点搜到一个点访 ...

  8. hdu 1599 find the mincost route

    http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. #include <cstdio> #include <cstri ...

  9. HDU 1599 find the mincost route (最短路 floyd)

    题目链接 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....V ...

随机推荐

  1. springbootDay03 cookie和session 购物车技术

    一.会话技术 1. 什么是会话 在计算机术语中,会话指的是客户端和服务器交互通讯的过程.简单的理解,大家可以看成是两个普通的人在打电话.一次电话从通话开始到挂断,可以看成是会话. 会话的特征 会话能够 ...

  2. 批量部署ssh免密登陆

    #!/bin/bash#set -xservers="10.254.192.xx10.254.192.xx10.254.192.xx"passwd="xxxxxxxx&q ...

  3. python3.6操作mysql

    1.通过 pip 安装 pymysql 进入 cmd  输入  pip install pymysql   回车等待安装完成: 安装完成后出现如图相关信息,表示安装成功. 2.测试连接 import ...

  4. 【bzoj4010】[HNOI2015]菜肴制作 拓扑排序+堆

    题目描述 给你一张有向图,问:编号-位置序(即每个编号的位置对应的序列)最小(例如1优先出现在前面,1位置相同的2优先出现在前面,以此类推)的拓扑序是什么? 输入 第一行是一个正整数D,表示数据组数. ...

  5. hdu 1172 猜数字

    猜数字 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  6. 三十道DP练习(持续更新)(pw:DP)

    前言: 话说DP这种纯考思维的题目,总是让我很伤脑筋,一些特别简单的DP我都常常做不出来,所以革命从现在(2018-05-01)开始,努力多刷点DP的练习-. 1.顺序对齐(align) 时间:201 ...

  7. Windows2008下RDP采用私有CA服务器证书搭建文档

    在中小型公司建立企业根证书颁发机构 (CA) http://www.microsoft.com/china/smb/issues/sgc/articles/build_ent_root_ca.mspx ...

  8. 转:LinkedHashMap使用(可以用来实现LRU缓存)

    1. LinkedHashMap概述: LinkedHashMap是HashMap的一个子类,它保留插入的顺序,如果需要输出的顺序和输入时的相同,那么就选用LinkedHashMap. LinkedH ...

  9. 自己写的enum转换的一个扩展,

    public static String ToEnumName(this int? source, Type e) { if (!source.HasValue) throw new Argument ...

  10. webservice跨域文件,好多年前的东西,远程调用,js服务器端使用,可以远程调用

    1.clientaccesspolicy.xml <?xml version="1.0" encoding="utf-8" ?> <acces ...