题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599

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.
题目大意:给一张n个点,m条无向边的图,求图中最小环长度。
解题思路:
1.floyd最小环模板
2.注意inf为0x3f3f3f3f时,三个inf相加会爆int ,所以数组我们开long long型
代码如下:
 #include<stdio.h>
#include<algorithm>
#define LL long long
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const LL inf = 0x3f3f3f3f;
const int MAXN = ; int n, m;
LL dis[MAXN][MAXN], map[MAXN][MAXN];//最短路径 直接路径
LL ans; void floyd()
{
ans = inf;
for(int k = ; k <= n; k ++) //枚举中点
{
for(int i = ; i < k; i ++) //起点
for(int j = i + ; j < k; j ++)//终点
ans = min(ans, map[i][k] + map[k][j] + dis[i][j]); //在dis没更新倒k之前,是没有经过k点的。所以保证了至少3个不同的点
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
if(ans != inf)
printf("%lld\n", ans);
else
printf("It's impossible.\n");
} int main()
{
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i = ; i <= n; i ++)
for(int j = ; j <= n; j ++)
{
if(i == j)
dis[i][j] = map[i][j] = ;
else
dis[i][j] = map[i][j] = inf;
}
for(int i = ; i <= m; i ++)
{
int a, b;
LL c;
scanf("%d%d%lld", &a, &b, &c);
dis[a][b] = dis[b][a] = map[a][b] = map[b][a] = min(map[a][b], c);//双向边
}
floyd();
}
return ;
}

对于有向图的最小环:不同之处在于1.建图(不用说了吧 有向图的建图)2. ans = min(ans, map[i][k] + map[k][j] + dis[j][i]);

find the mincost route【无向图最小环】的更多相关文章

  1. find the mincost route(最小环,最短路,floyd)

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

  2. HD1599 find the mincost route(floyd + 最小环)

    题目链接 题意:求最小环 第一反应时floyd判断,但是涉及到最少3个点,然后就不会了,又想的是 双联通分量,这个不知道为什么不对. Floyd 判断 最小环 #include <iostrea ...

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

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

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

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

  7. [hdu P1599] find the mincost route

    [hdu P1599] find the mincost route 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V ...

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

  9. hdu1599 find the mincost route floyd求出最小权值的环

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

  10. POJ 1734 无向图最小环/有向图最小环

    给定一张图,求图中一个至少包含三个点的环,环上的点不重复,并且环上的边的长度之和最小. 点数不超过100个 输出方案 无向图: /*Huyyt*/ #include<bits/stdc++.h& ...

随机推荐

  1. HR# 5题解

    T1 我傻了 前20个数暴力开桶记录,后面的每次暴力统计. #include<bits/stdc++.h> #define R register int using namespace s ...

  2. vue中使用ckeditor,支持wps,word,网页粘贴

    由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...

  3. ActiveMQ C#实例

    重点参考:NMS Documentation 一.ActiveMQ Queue 在ActiveMQ中Queue是一种点对点的消息分发方式,生产者在队列中添加一条消息,然后消费者消费一条消息,这条消息保 ...

  4. leetcode解题报告(8):Remove Element

    描述 Given an array and a value, remove all instances of that value in place and return the new length ...

  5. React前端开发环境搭建

    先,我们需要明确的是React和很多前端框架一样,底层都还是js以及html,即便它有着看似特殊的jsx语法. 我们要在服务端运行js,就需要依赖一个环境,和运行war包需要tomcat一类中间件一样 ...

  6. RuntimeException异常处理汇总

    Java中所有异常的父类是Throwable类,在Throwable类下有两大子类: 一个是Error类,指系统错误异常,例如:VirtualMachineError 虚拟机错误,ThreadDeat ...

  7. Flask 四种响应类型

    1 直接返回字符串 可以返回状态码 @app.route('/testresponse', methods=['GET', 'POST']) def testresponse(): return &q ...

  8. 【git 命令总结】一

    git help config init commit diff rename mv rm head revert reset branch checkout branch-diff fast-for ...

  9. mysql CONCAT函数

    有时候我们需要使用coacat函数拼接一些字段的生成一个字符串,比如:select concat(field1,field2,field3)  from xxx: 这时候我们就会的到一个这些字段的值拼 ...

  10. OpenResty之ngx_lua模块的加密接口

    原文: ngx_Lua模块中的加密api接口 ngx.crc32_short digest = ngx.crc32_short(str) 该方法主要是计算给定字符串 str 的循环校验码(Cyclic ...