传送门:

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. vue的简单测试

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. C#与.NET的区别和C#程序结构

    C#语言及其特点 (1)语法简洁,不允许直接操作做内存,去掉指针操作 (2)彻底的面向对象设计,C#具有面向对象所应用的一切特性:封装.继承.多态 (3)与Web紧密结合,C#支持绝大多数的Web标准 ...

  3. PoPo数据可视化周刊第4期

    PoPo数据可视化 聚焦于Web数据可视化与可视化交互领域,发现可视化领域有意思的内容.不想错过可视化领域的精彩内容, 就快快关注我们吧 :) 微信号:popodv_com   由于国庆节的原因,累计 ...

  4. Signal & Slot in Qt

    Try your best to provide an mechanism to implement what you want. 1. All is generated by QT Framewor ...

  5. 【Android Studio使用教程1】Android Studio导入项目的几种方法

    本篇教程中使用到的Android Studio版本为1.0, Eclipse ADT版本23.0.4.请尝试更新到该版本. Android Studio默认使用 Gradle 构建项目, Eclips ...

  6. 《你不知道的JavaScript-上卷》笔记

    这段时间看了<你不知道的JavaScript>上卷,对很多知识有了重新的认识,所以在这里罗列一些知识点作为巩固. 作用域和闭包 词法作用域 变量赋值操作会执行的两个动作 答:编译器会在当前 ...

  7. mac下 IDEA 的pom下 出现 Cannot access in offline mode 问题

    在mac下 配置完maven后发现总是不能引入最新的jar包,google了好久总算找到解决办法: 默认带有work offline ,不清楚这个是干嘛用的.勾选掉 了就行了.

  8. Linux常用命令(随时补充)

    1.系统 1.1.系统关闭.重启 1)关闭:shutdown -h now 2)重启:reboot.init 6 1.2.修改默认网卡 1)vi /etc/udev/rules.d/70-persis ...

  9. matplotlib点线 坐标刻度 3D图绘制(六)

    plot语句中支持除X,Y以外的参数,以字符串形式存在,来控制颜色.线型.点型等要素,语法形式为: plt.plot(X, Y, 'format', ...) 1 点和线的样式 颜色 参数color或 ...

  10. 【Leetcode】【Hard】Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...