[hdu P1599] find the mincost route
[hdu P1599] find the mincost route
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.
这是一道无向图最小环的模板题。看来我不会的还有挺多的啊。。。
无向图最小环就是在一张无向图当中,找出一个点数大于等于3的简单环,其权值最小。
这个方法还是比较巧妙的,用floyd相当于动归思想O(n^3)的复杂度算出来(路径也可算)。
如果用dij则要O(n^4)。
这。。应该算是一个模板吧。
唯一要说的,就是这题三个值相加,0x3f3f3f3f*3会爆int。
code:
#include<bits/stdc++.h>
#define Ms(a,x) memset(a,x,sizeof a)
using namespace std;
;
int n,m,w[N][N],f[N][N],ans;
int main() {
while (scanf("%d%d",&n,&m)!=EOF) {
Ms(w,),Ms(f,),ans=f[][];
,x,y,z; i<=m; i++) {
scanf("%d%d%d",&x,&y,&z);
w[x][y]=z<w[x][y]?z:w[x][y],w[y][x]=w[x][y];
}
; i<=n; i++)
; j<=n; j++) f[i][j]=w[i][j];
; k<=n; k++) {
; i<k; i++)
; j<k; j++)
ans=min(ans,f[i][j]+w[i][k]+w[k][j]);
; i<=n; i++)
; j<=n; j++)
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
}
][]) printf("%d\n",ans);
else puts("It's impossible.");
}
;
}
[hdu P1599] find the mincost route的更多相关文章
- 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 ...
- 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 ...
- hdu 1599 find the mincost route(无向图的最小环)
find the mincost route Time Limit: 1000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 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 ...
- hdu 1599 find the mincost route
http://acm.hdu.edu.cn/showproblem.php?pid=1599 floyd找最小环. #include <cstdio> #include <cstri ...
- hdu 1599 find the mincost route 最小环
题目链接:HDU - 1599 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须 ...
- HDU 1599 find the mincost route (无向图的最小环)
题意: 给一个带权无向图,求其至少有3个点组成的环的最小权之和. 思路: (1)DFS可以做,实现了确实可以,只是TLE了.量少的时候应该还是可以水一下的.主要思路就是,深搜过程如果当前点搜到一个点访 ...
- hdu 1599 find the mincost route(flyod求最小环)
Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1, ...
- HDU 1599 find the mincost route (无向图floyd最小环详解)
转载请注明出处:http://blog.csdn.net/a1dark 分析:终于弄懂了floyd的原理.以前的理解一直肤浅.所以一做到floyd应用的题.就拙计了.其实floyd的本质DP.利用前K ...
随机推荐
- 【托业】【新东方托业全真模拟】TEST05~06-----P5~6
credit A with B 把A归功于B present A with B 给A赠送B proofread thoroughly 彻底地校对:exclusively 专门地:独占地:apparen ...
- Redis入门到高可用(十九)——Redis Sentinel
一.Redis Sentinel架构 二.redis sentinel安装与配置 四.客户端连接Sentinel 四.实现原理—— 故障转移演练(客户端高可用) 五.实 ...
- 利用python脚本(re)抓取美空mm图片
很久没有写博客了,这段时间一直在搞风控的东西,过段时间我把风控的内容整理整理发出来大家一起研究研究. 这两天抽空写了两个python爬虫脚本,一个使用re,一个使用xpath. 直接上代码——基于re ...
- vue安装,router-link的一些属性,用法,tag active-class,to,replace,exex等等
第一步:$ npm install -g vue-cli 第二部:$ vue init webpack my-projectName 下面内容转载自:https://www.cnblogs.com/c ...
- CentOS 7 DR模式LVS搭建
调度器LB : 192.168.94.11 真实web服务器1 : 192.168.94.22 真实web服务器2 : 192.168.94.33 VIP : 192.168.94.111 脚本如下 ...
- Painter's Problem (高斯消元)
There is a square wall which is made of n*n small square bricks. Some bricks are white while some br ...
- noip单词接龙
看了许多题解都好长啊,自不量力的来贴一下代码 (震惊于这都能ac...) 这道题的思路是先从字符串中找有重部分然后直接比较剩下的部分,比较的数据也可以用来计算数值 其实满水的题 总之看注释啦(竟然能耐 ...
- bash vim等常用命令
生成的txt一不小心带了./,用vim: :%s/\.\/single/single #%s/xxx/yyy/g是全文本替换,这里用\将特殊字符.和/转换成普通字符 结果还不错: ---------- ...
- es6 复制对象
var pp = {'name': '1','work': 'teacher'} var kk = [1,2] var tt = [] for(let index = 0; index<kk.l ...
- flask框架----信号
一.实例化补充 instance_path和instance_relative_config是配合来用的.这两个参数是用来找配置文件的,当用app.config.from_pyfile('settin ...