Floyd 最小环模板题

code

/*
floyd最小环,记录路径,时间复杂度O(n^3)
不能处理负环
*/
#include <iostream>
#include <cstring>
using namespace std;
const int INF = 109, maxn = 252645135;
int g[INF][INF], dis[INF][INF], pre[INF][INF];
int ans[INF];
//pr[i][j]记录i到j最短路径的第一个点
int n, m, tem , tol;
int main() {
while (cin >> n >> m) {
tem = maxn;
memset (g, 0xf, sizeof g);
memset (dis, 0xf, sizeof dis);
memset (pre, 0, sizeof pre);
int x, y, z;
for (int i = 1; i <= m; i++) {
cin >> x >> y >> z;
if (z < g[x][y])
dis[x][y] = dis[y][x] = g[x][y] = g[y][x] = z;
pre[x][y] = y, pre[y][x] = x;
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i < k; i++)
for (int j = i + 1; j < k; j++) {
if (tem > dis[i][j] + g[i][k] + g[k][j]) {
tem = dis[i][j] + g[i][k] + g[k][j];
int t = i;
tol = 0;
while (t != 0) {
ans[++tol] = t;
t = pre[t][j];
}
ans[++tol] = k;
}
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j && dis[i][k] + dis[k][j] < dis[i][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
pre[i][j] = pre[i][k];
}
}
if (tem == maxn) cout << "No solution.";
else
for (int i = 1; i <= tol; i++)
cout << ans[i] << ' ';
cout << endl;
}
return 0;
}

  

POJ 1734.Sightseeing trip (Floyd 最小环)的更多相关文章

  1. POJ 1734 Sightseeing trip(Floyd)

    题目传送门 题目中文翻译: Description 桑给巴尔岛上的阿德尔顿镇有一家旅行社,它已决定为其客户提供除了许多其他名胜之外的景点.为了尽可能地从景点赚取收入,该机构已经接受了一个精明的决定:有 ...

  2. POJ 1734 Sightseeing trip(无向图最小环+输出路径)

    题目链接 #include <cstdio> #include <string> #include <cstring> #include <queue> ...

  3. poj 1734 Sightseeing trip判断最短长度的环

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5590   Accepted: 2151 ...

  4. POJ 1734 Sightseeing trip

    题目大意: 求一个最小环. 用Floyd 求最小环算法. #include <iostream> #include <cstdlib> #include <cstdio& ...

  5. poj1734 Sightseeing trip【最小环】

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:8588   Accepted:3224   ...

  6. URAL 1004 Sightseeing Trip(最小环)

    Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...

  7. poj 1734 Sightseeing trip_ 最小环记录路径

    题意:求最出小环,输出路径 #include <iostream> #include<cstdio> using namespace std; #define N 110 #d ...

  8. #10072. 「一本通 3.2 例 1」Sightseeing Trip(floyd求最小环+路径)

    https://loj.ac/problem/10072 针对无向图 因为Floyd是按照结点的顺序更新最短路的,所以我们在更新最短路之前先找到一个连接点k,当前的点k肯定不存在于已存在的最短路f[i ...

  9. poj1734 Sightseeing trip(Floyd求无向图最小环)

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

随机推荐

  1. apache 服务发布多个项目,只需要更改配置文件(需要设定虚拟主机)

    http://www.php186.com/content/article/apache/24609.html http://blog.sina.com.cn/s/blog_6b689d5901013 ...

  2. bzoj 2286 [Sdoi2011]消耗战(虚树+树上DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2286 [题意] 给定一棵树,切断一条树边代价为ci,有m个询问,每次问使得1号点与查询 ...

  3. 2013.08.23.diary

    Today, my baby called me.She said, she want to go abroad . And she wants me to go abroad too. I thin ...

  4. [转载]jQuery UI 使用

    最近项目中使用了一些插件来做页面,这里把jQuery UI的使用分享出来,希望 对新手有帮助.文章结尾附源码下载. 1     jQuery UI 2     为我所用 2.1     Tabs 2. ...

  5. [LeetCode] Largest Rectangle in Histogram 解题思路

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. pecl/mongo is already installed

    sw-engine-cgi PHP MongoDB database drivermongodb database driver,数据库驱动;

  7. Java程序员必知的8大排序算法

    8种排序之间的关系 直接插入排序 (1)基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排 好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数 也是排好顺序的.如 ...

  8. 软中断与硬中断 & 中断抢占 中断嵌套

    参考了这篇文章:http://blog.csdn.net/zhangskd/article/details/21992933 从本质上来讲,中断是一种电信号,当设备有某种事件发生时,它就会产生中断,通 ...

  9. 面试题 php随机获取概率结果

    题目:随机输出“苹果”,“橘子”,“香蕉”要求输出“苹果”的概率为50%,“橘子”的概率为30%,“香蕉”的概率为20% 分析 方案一: 最常用rand(1,10)来处理 如果是5以下的输出苹果 6到 ...

  10. Linux命令之hwclock - 查询和设置硬件时钟

    常用参数 -r, --show         读取并打印硬件时钟(read hardware clock and print result ) -s, --hctosys      将硬件时钟同步到 ...