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>
#include<iostream>
using namespace std;
#define maxn 123
#define inf 1e8
int dis[maxn][maxn],n,key;
int gra[maxn][maxn],m,id;
int u,v,w,pre[maxn][maxn],ans[maxn];
void floyd()
{
    key=inf;
    for(int k=1; k<=n; k++)
    {
        for(int i=1; i<k; i++)
        {
            for(int j=i+1; j<k; j++)
            {
                int tmp=dis[i][j]+gra[i][k]+gra[k][j];
                if(tmp<key)
                {
                    key=tmp;
                    id=0;
                    int p=j;
                    while(p!=i)
                    {
                        ans[id++]=p;
                        p=pre[i][p];
                    }
                    ans[id++]=i;
                    ans[id++]=k;
                }
            }
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(dis[i][j]>dis[i][k]+dis[k][j])
                {
                    dis[i][j]=dis[i][k]+dis[k][j];
                    pre[i][j]=pre[k][j];
                }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        key=inf;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
            {
                gra[i][j]=dis[i][j]=inf;
                pre[i][j]=i;
            }
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            gra[u][v]=gra[v][u]=dis[u][v]=dis[v][u]=min(gra[u][v],w);
        }
        floyd();
        if(key==inf)printf("No solution.\n");
        else
        {
            printf("%d",ans[0]);
            for(int i=1; i<id; i++)
                printf(" %d",ans[i]);
            printf("\n");
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/SDUTNING/p/10310808.html

时间: 2024-11-06 14:02:13

Sightseeing trip POJ - 1734 -Floyd 最小环的相关文章

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

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

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

POJ1734 Sightseeing trip【Floyd】【最小环】

Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5038 Accepted: 1930 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(floyd求最小环)

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6491   Accepted: 2486   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

POJ1734 Sightseeing trip 【Floyd】+【最小环】+【路径记录】

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4830   Accepted: 1857   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

URAL 1004 Sightseeing Trip(最小环)

Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB 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 a