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 (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7047 Accepted Submission(s): 2736
接下来的M行里,每行包括3个整数a,b,c.代表a和b之间有一条通路,并且需要花费c元(c <= 100)。
1 2 1
2 3 1
1 3 1
3 3
1 2 1
1 2 3
2 3 1
It's impossible.
#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求最小环 无向图)的更多相关文章
- 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 (最小环与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(flyod求最小环)
Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1, ...
- hdu1599 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(无向图的最小环)
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 (无向图floyd最小环详解)
转载请注明出处:http://blog.csdn.net/a1dark 分析:终于弄懂了floyd的原理.以前的理解一直肤浅.所以一做到floyd应用的题.就拙计了.其实floyd的本质DP.利用前K ...
- HDU 1599 find the mincost route (最短路 floyd)
题目链接 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....V ...
随机推荐
- Java SE 8 的流库学习笔记
前言:流提供了一种让我们可以在比集合更高的概念级别上指定计算的数据视图.如: //使用foreach迭代 long count = 0; for (String w : words) { if (w. ...
- BZOJ2656 [Zjoi2012]数列
Description 小白和小蓝在一起上数学课,下课后老师留了一道作业,求下面这个数列的通项公式: $$\begin{aligned}A_0 &= 0\\A_1 &= 1\\A_{2 ...
- Spring与Web整合
一 概述 1.整合目的 将所有对象的创建与管理任务交给Spring容器,降低程序的耦合度. 2.整合途径 将Spring容器注入到Web容器中. 3.具体实现 使用ServletContextList ...
- 理解js中bind方法的使用
提到bind方法,估计大家还会想到call方法.apply方法:它们都是Function对象内建的方法,它们的第一个参数都是用来更改调用方法中this的指向.需要注意的是bind 是返回新的函数,以便 ...
- IhyerDB modBus采集器配置.
近期查了一下ihyerDB-modbus采集器的相关配置,由于没有相关的modbus设备,于是今天上午根据网上的线索下载了Modbus Slave(modbus从站仿真器).笔记本也没有串口,于是下载 ...
- A memory map of an object
Phone类 package com.itheima_02; /* * 手机类: * 成员变量:品牌,价格,颜色... * 成员方法:打电话,发短信... */ public class Phone ...
- 读<css世界>笔记之img标签
Web开发时,为了节约带宽以及提高加载性能,首屏以下的图片就会通过滚屏加载的方式异步加载,然后这个即将被异步加载的图片为了布局稳健,体验良好,往往会使用一张透明的图片占位,如: <img src ...
- 面向对象进阶------>内置函数 str repr new call 方法
__new__方法: 我们来讲个非常非常重要的内置函数和init一样重要__new__其实在实例话对象的开始 是先继承父类中的new方法再执行init的 就好比你生孩子 先要把孩子生出来才能对孩子 ...
- Java学习---InetAddress类的学习
基础知识 1.InetAddress类 在网络API套接字,InetAddress类和它的子类型对象使用域名DNS系统,处理主机名到主机IPv4或IPv6地址的转换.如图1-1所示. 由于InetAd ...
- 2 Docker 镜像基础
Docker 镜像可以从docker.io 下载,也可以自己通过Dockerfile来构建镜像,我有时从国外下载镜像时,网速不行,我就改成国内的镜像,修改如下: # vim /etc/docker/d ...