HDU-1599-find the mincost route
来源:互联网 发布:js只选择小时分钟控件 编辑:程序博客网 时间:2024/06/06 20:43
ACM模版
描述
题解
求最小环,题上说的十分清楚,是无向图,所以要求环的结点最起码要有三个,也就是说,K>2,如果是有向图,那么结点最少可以是两个……
模版题,直接套~~~用Floyd求解。
代码
#include <iostream>using namespace std;const int INF = 0x3f3f3f3f;const int MAXN = 110;int n, m; // n:节点个数, m:边的个数int g[MAXN][MAXN]; // 无向图int dist[MAXN][MAXN]; // 最短路径int r[MAXN][MAXN]; // r[i][j]: i到j的最短路径的第一步int out[MAXN], ct; // 记录最小环int solve(int i, int j, int k){ // 记录最小环 ct = 0; while (j != i) { out[ct++] = j; j = r[i][j]; } out[ct++] = i; out[ct++] = k; return 0;}int main(){ while (scanf("%d%d", &n, &m) != EOF) { int i, j, k; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { g[i][j] = INF; r[i][j] = i; } } for (i = 0; i < m; i++) { int x, y, l; scanf("%d%d%d", &x, &y, &l); --x; --y; if (l < g[x][y]) { g[x][y] = g[y][x] = l; } } memmove(dist, g, sizeof(dist)); int min_ = INF; // 最小环 for (k = 0; k < n; k++) { // Floyd for (i = 0; i < k; i++) // 一个环中的最大结点为k(编号最大) { if (g[k][i] < INF) { for (j = i + 1; j < k; j++) { if (dist[i][j] < INF && g[k][j] < INF && min_ > dist[i][j] + g[k][i] + g[k][j]) { min_ = dist[i][j] + g[k][i] + g[k][j]; solve(i, j, k); // 记录最小环 } } } } for (i = 0; i < n; i++) { if (dist[i][k] < INF) { for (j = 0; j < n; j++) { if (dist[k][j] < INF && dist[i][j] > dist[i][k]+dist[k][j]) { dist[i][j] = dist[i][k] + dist[k][j]; r[i][j] = r[k][j]; } } } } } if (min_ < INF) { printf("%d", min_); } else { printf("It's impossible."); } printf("\n"); } return 0;}
参考
《Floyd求最小环》
0 0
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- hdu 1599 find the mincost route
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- HDU 1599 find the mincost route
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- hdu 1599 find the mincost route
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- hdu 1599 find the mincost route
- HDU 1599 find the mincost route
- 【HDU 1599 find the mincost route】
- HDU 1599 find the mincost route
- 【HDU 1599 find the mincost route 】
- HDU 1599 find the mincost route
- HDU-1599-find the mincost route
- 腾讯企业邮发送邮件 java版
- 最全的常用正则表达式大全
- javaweb中乱码问题
- 矩阵快速幂-- 挑战程序设计
- 进击的KFC : View使用strong还是weak
- HDU-1599-find the mincost route
- PHP中的ob_start()等函数
- android M Launcher之LauncherModel (三)
- NOIP2016备赛试题
- 数据结构——树的水平遍历
- python数据结构学习笔记-2016-11-26-01-链表排序
- 线程管理之---信号量的操作
- java 堆栈
- 线程的状态与调度