find the mincost route

Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3885    Accepted Submission(s):
1559

Problem Description
杭州有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.".
 
Sample Input
3 3
1 2 1
2 3 1
1 3 1
3 3
1 2 1
1 2 3
2 3 1
 
Sample Output
3
It's impossible.
 
Author
8600
 
Source
 
Recommend
8600   |   We have carefully selected several similar
problems for you:  1595 1598 1596 1142 1162 
 
题目为求无向图的最小环,因为数据范围小于100,所以用floyd计算,方法非常巧妙,可以作为模板题学习。
 
题意:中文题,很好理解。
 
附上代码:
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#define M 1005
#define MAX 1000000
using namespace std;
int map[M][M],dis[M][M];
int n; void floyd()
{
int i,j,k;
int ans=MAX;
for(k=; k<=n; k++)
{
for(i=; i<k; i++)
for(j=i+; j<k; j++)
if(ans>map[i][k]+map[k][j]+dis[j][i]) //注意这里的写法
ans=map[i][k]+map[k][j]+dis[j][i];
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(dis[i][j]>dis[i][k]+dis[k][j])
dis[i][j]=dis[i][k]+dis[k][j];
}
if(ans<MAX)
printf("%d\n",ans);
else
printf("It's impossible.\n");
} int main()
{
int m,i,j;
while(~scanf("%d%d",&n,&m))
{
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
if(i==j) map[i][j]=dis[i][j]=;
else map[i][j]=dis[i][j]=MAX;
}
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=dis[a][b]=dis[b][a]=c;
}
floyd();
}
return ;
}

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 floyd求无向图最小环

    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 最小环

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

  5. hdoj 1599 find the mincost route【floyd+最小环】

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

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

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

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

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

  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(flyod求最小环)

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

随机推荐

  1. 使用log4j打印日志

    在项目中我们必不可少需要打印日志,通过日志我们可以查看系统的运行状态是否正常,当程序出现异常的时候,我们也可以通过查看日志来定位问题的位置,给程序员的工作带来了极大的便利. 以下这边博客的内容是我从一 ...

  2. jquery tooltip.js

    1.引用关联的js脚本 <script type="text/javascript" src="script/jquery-1.3.2.min.js"&g ...

  3. Faster RCNN算法训练代码解析(3)

    四个层的forward函数分析: RoIDataLayer:读数据,随机打乱等 AnchorTargetLayer:输出所有anchors(这里分析这个) ProposalLayer:用产生的anch ...

  4. toString方法和valueof()方法的区别

    JavaScript引用类型之Array数组的toString()和valueof()方法的区别   一.转换方法 1.在JavaScript中几乎所有对象都具有toLocaleString().to ...

  5. ORACLE 使用笔记

    ORACLE TRUNC()函数 TRUNC():类似截取函数,按指定的格式截取输入的数据. 1.[trunc(for dates)]TRUNC()函数处理日期 语法格式:TRUNC(date[,fm ...

  6. Direct2D 第3篇 绘制文字

    原文:Direct2D 第3篇 绘制文字 #include <windows.h> #include <d2d1.h> #include <d2d1helper.h> ...

  7. jsp之jstl(展示所有商品、重写登录案例)

    jsp之jstl jstl: jsp标准的标签库语言,apache的,是用来替代java脚本 使用步骤: 1.导入jar包 (jstl.jar和standard.jar) 2.在页面上导入标签库 &l ...

  8. Javascript一些要点记录

    1. == 比较,它会自动转换数据类型再比较 === 比较,它不会自动转换数据类型,如果数据类型不一致,返回false 大部分时候应该使用===来比较2. 使用'use strict'来强制通过var ...

  9. var与let循环中经典问题

    循环1: 下面代码运行结果是输出10   <script> var a =[]; for(var i = 0;i<10;i++){ a[i] = function(){ consol ...

  10. jquery的innerWidth 和 innerHeight的使用

    innerWidth This method returns the width of the element, including left and right padding, in pixels ...