传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1599

find the mincost route

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

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 1217 1597 
 
分析:
无向图
必须3个点才能成环
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
using namespace std;
typedef long long LL;
#define max_v 105
#define INF 0x7ffffff
int G[max_v][max_v];
int dis[max_v][max_v];
int m,n,minc;
void floyd()
{
minc=INF;
for(int k=;k<=n;k++)//前K-1个点的情况递推前K个点的情况
{
for(int i=;i<=k;i++)
{
for(int j=i+;j<=k;j++)//i j两个点必然不同
{
minc=min(minc,dis[i][j]+G[i][k]+G[k][j]);//K为环的最大点、无向图三点成环(从k点出发,回到k点)
}
}
for(int i=;i<=n;i++)//floyd算法求任意两点的最短路、包含前K-1个点
{
for(int j=;j<=n;j++)
{
if(dis[i][j]>dis[i][k]+dis[k][j])
{
dis[i][j]=dis[i][k]+dis[k][j];
}
}
}
}
}
void init()//初始化必须全部都为无穷大、因为自身不能成环
{
for(int i=;i<max_v;i++)
{
for(int j=;j<max_v;j++)
{
G[i][j]=INF;
dis[i][j]=INF;
}
}
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
init();
int s,e,v;
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&s,&e,&v);
if(v<G[s][e])//重边
{
G[s][e]=G[e][s]=v;
dis[s][e]=dis[e][s]=v;
}
}
floyd();
if(minc<INF)
printf("%d\n",minc);
else
printf("It's impossible.\n");
}
return ;
}

HDU 1599 find the mincost route(floyd求最小环 无向图)的更多相关文章

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

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

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

  4. hdu1599 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(无向图的最小环)

    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

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

  7. hdu 1599 find the mincost route 最小环

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

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

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

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

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

随机推荐

  1. UGUI——重写Image类实现进度条

    目的: 游戏中经常会用到进度条,但是美术给的图片用filled一拉伸就很难看,如下图 第一种模式是九宫格模式,第二种是filled.而我们需要的是两种可结合的. 如何实现: 新建一个类,继承image ...

  2. php中模糊查询并关联三个select框

    1.在php中我们经常用到下拉框,并相互关联,如果下拉框的option非常多,那么我们就要用到模糊搜索功能,那么怎么做呢? 在此功能中,走了弯路,最好不要关联两个select的id值后select属性 ...

  3. luogu P3065 first——trie树相关

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  4. mqtt server搭建和web中使用js-sdk订阅发布消息

    1.mqtt server搭建(From:https://www.cnblogs.com/huhongy/p/7929299.html) window安装MQTT服务器,我这里下载了一个apache- ...

  5. HTTP(S)网络框架的设计

    0.麻烦 操作系统提供的网络接口都会令人不爽,要么太接近底层而使用不便,要么层次过高又不提供底层点的接口供设置参数.但是我们不能期望系统API做得很高级,因为没有绝对合适的网络库,必须定制化从而达到适 ...

  6. MAVLink Linux/QNX/MacOs Integration Tutorial (UDP)

    MAVLink Linux/QNX/MacOs Integration Tutorial (UDP) Overview This program was written to test the udp ...

  7. 修改zabbix为中文,并解决乱码问题(三)

    当Zabbix安装完成后,默认则是英文界面,有的人看英文不习惯,现在将其改为中文界面 一.修改为中文 1.打开Zabbix界面,Administrator-Users 选择语言-Chinese(zh_ ...

  8. new era

    新的博客,将会记录我在工作和学习中遇到的问题以及总结...

  9. 自动化测试基础篇--Selenium unittest生成测试报告(HTMLTestRunner)

    如何生成HTMLTestRunner测试报告.接上篇文章,对于unittest框架,运行后,测试结果不便于查看,同时多个case存在的时候,可能会导致case result记录不正确的情况. 为此,引 ...

  10. 实现UIView的无限旋转动画(非CALayer动画)

    实现UIView的无限旋转动画(非CALayer动画) 效果: 素材: 源码: // // ViewController.m // Animation // // Created by YouXian ...