POJ 1734

被计组实验真冲昏了头脑。。水一发先。

最小环+记录路径;

几个sb点。题目没看,以为是单向边。每次记录路径递归写重(开头结尾多记录一遍)。重边忘处理。

#include <cstdio>
#include <cstring>
#define INF 10000000

using namespace std;

int n, m, x, y, mini, minj, g[105][105], k;
int w[105][105], f[105][105], ans[105], min;
int now;

void find(int x, int y)
{
    if(g[x][y] == 0)
        return;
    find(x, g[x][y]);
    ans[now ++] = g[x][y];
    find(g[x][y], y);
}

int main()
{
    scanf("%d%d", &n, &m);
    min = INF;
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
        {
            w[i][j] = INF;
            f[i][j] = INF;
        }
    for(int i = 0; i < m; i ++)
    {
        scanf("%d%d", &x, &y);
        scanf("%d", &k);
        if(f[x][y] > k)
            f[x][y] = f[y][x] = w[y][x] = w[x][y] = k;
    }

    for(int k = 1; k <= n; k ++)
    {
        mini = 0, minj = 0;
        for(int i = 1; i < k; i ++)
            for(int j = 1; j < k; j ++)
                if(i != j)
                if(min > w[k][i]+f[i][j]+w[j][k])
                {
                    min = w[k][i]+f[i][j]+w[j][k];
                    mini = i, minj = j;
                }
        // printf("%d %d %d %d\n", k, min, mini, minj);
        if(mini != 0)
        {
            ans[0] = minj;
            ans[1] = k;
            ans[2] = mini;
            now = 3;
            find(mini, minj);
        }
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                if(f[i][j] > f[i][k]+f[k][j])
                {
                    f[i][j] = f[i][k]+f[k][j];
                    g[i][j] = k;
                }
    }
    // printf("%d\n", min);
    if(min < INF)
        for(int i = 0; i < now; i ++)
            if(i != now-1)
                printf("%d ", ans[i]);
            else
                printf("%d\n", ans[i]);
    else
        printf("No solution.\n");
    return 0;
}

时间: 2024-09-29 23:10:56

POJ 1734的相关文章

Sightseeing trip POJ - 1734 -Floyd 最小环

POJ - 1734 思路 : Floyd 实质 dp ,优化掉了第三维. dp [ i ] [ j ] [ k ] 指的是前k个点优化后    i  ->  j   的最短路. 所以我们就可以利用这个性质去求 最小环,最小环的构成可以看作是由一条  i -> k    k->j   加上 dp [ i ] [ j ]的最短路 那么我们可以利用  还没有用 k 优化的  i - >j 的最短路 去求,这样保证了 ,这是一个真正的环. #include<stdio.h>

POJ 1734 Sightseeing trip (Floyd 最小环+记录路径)

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5040   Accepted: 1932   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

POJ 1734.Sightseeing trip 解题报告

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最短路径的第一个点 i

POJ 1734:Sightseeing trip

Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6831 Accepted: 2612 Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions

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

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5590   Accepted: 2151   Special Judge Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attra

POJ 1734 Sightseeing trip

题目大意: 求一个最小环. 用Floyd 求最小环算法. #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring> using namespace std; #define

POJ 1734 无向图最小环/有向图最小环

给定一张图,求图中一个至少包含三个点的环,环上的点不重复,并且环上的边的长度之和最小. 点数不超过100个 输出方案 无向图: 1 /*Huyyt*/ 2 #include<bits/stdc++.h> 3 #define mem(a,b) memset(a,b,sizeof(a)) 4 #define pb push_back 5 using namespace std; 6 typedef long long ll; 7 typedef unsigned long long ull; 8

Sightseeing trip (POJ - 1734)

There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this attraction, the agency has accepted a shrewd decision: it is

ACM图论—最小环问题 ( 仔细分析+理解+代码 )(HDU 1599 ) (POJ 1743)

说明:如果发现错误或者有任何问题,任何不理解的地方请评论提出,或私信me,^ _ ^ ACM-图论 最小环问题(Floyd算法应用) 最小环问题是Floyd算法的应用,并不难,和Floyd算法一样难度.但是如果要输出最小环路径就要稍微麻烦一点,也不难. 1.计算最小环值(HDU 1599) 有向图最小环: 有向图最小环最少要有2个点组成环,这个的写法就是用Floyd()求最短距离,最后所有点中的最短距离的最小值就是答案. 无向图最小环: 肯定和有向环做法有区别,无向图构成环最少要有3个点,所以求