传送门:

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. Java基础小结

    JavaSE基础 本文为作者在学习和笔试题中遇到的小知识点总结,做以总结和备用. jdk的安装和配置环境变量 (1)以win10为例,右键此电脑,选择属性,进去系统设置,选择高级系统设置,进入环境变量 ...

  2. webpack-bundle.js原理

    bundle.js 源码 //a.js import { log } from './b.js' log('hello') //b.js export const log = function (m) ...

  3. DOM基础操作(一)

    DOM的基本操作有四种,我们会逐一给大家进行展示 增加操作 1.创建元素节点 createElement 我们可以通过document.createElement(‘div’);这个方法来创建一个元素 ...

  4. bzoj1201: [HNOI2005]数三角形----递推+bitset

    -by  bzoj http://www.lydsy.com/JudgeOnline/problem.php?id=1201 枚举所有交点,统计每个以每个点为顶点的正三角和和以每个点为左端点的反三角 ...

  5. Tomcat学习总结(一):目录简介

    一:下载地址和目录结构说明. Tomcat官方站点:http://jakarta.apache.org 下载Tomcat安装程序包:http://tomcat.apache.org/

  6. MySQL与PHP的连接教程步骤(图文)

    本篇文章我们介绍一下PHP与MySQL的整合,既然是与MySQL整合,那么我们首先肯定是要安装MySQL.下面我们就介绍下MySQL的安装方法. 第一步,下载MySQL.下载PHP可以去PHP中文网下 ...

  7. XAMPP添加二级域名

    1.在hosts中添加域名 2.点击config选择httpd.conf,去掉Include前面的#号 3.取消NameVirtualHost前面的##,在页面底端添加二级域名 4.重启xampp

  8. python模拟登陆豆瓣——简单方法

    学爬虫有一段时间了,前面没有总结又重装了系统,导致之前的代码和思考都没了..所以还是要及时整理总结备份.下面记录我模拟登陆豆瓣的方法,方法一登上了豆瓣,方法二重定向到了豆瓣中“我的喜欢”列表,获取了第 ...

  9. 微信小程序一个页面多个按钮分享怎么处理

    首先呢,第一步先看api文档: 组件:button https://developers.weixin.qq.com/miniprogram/dev/component/button.html 框架- ...

  10. 初识WCF1

    参照: http://blog.csdn.net/songyefei/article/details/7363296 第一篇 Hello WCF WCF(Windows Communication F ...