【POJ1734】Sightseeing Trip 无向图最小环

题目大意:给定一个 N 个顶点的无向图,边有边权,如果存在,求出该无向图的最小环,即:边权和最小的环,并输出路径。

题解:由于无向图,且节点数较少,考虑 Floyd 算法,在最外层刚开始遍历到第 K 号节点时,\(d[i][j]\) 中记录着经过前 k-1 个点,从 i 到 j 的最短距离。因此,可以依次考虑每一个结构:\(\{d[i][j]+G[i][k]+G[k][j] \}\),这便是一个环形结构,每次更新答案贡献即可。

至于路径输出,\(get\_path(int\ i,int\ j)\) 函数意为获得从节点 i 到节点 j 的中间节点,不包括端点是因为每次递归时会造成重复记录,因此需要手动额外记录端点。

代码如下

#include <bits/stdc++.h>
using namespace std;
const int maxn=110;
const int inf=0x3f3f3f3f;

inline int read(){
    int x=0,f=1;char ch;
    do{ch=getchar();if(ch==‘-‘)f=-1;}while(!isdigit(ch));
    do{x=x*10+ch-‘0‘;ch=getchar();}while(isdigit(ch));
    return f*x;
}

int n,m,ans=inf,G[maxn][maxn],d[maxn][maxn],mid[maxn][maxn];
vector<int> p;

void read_and_parse(){
    n=read(),m=read();
    memset(G,0x3f,sizeof(G));
    for(int i=1;i<=n;i++)G[i][i]=0;
    for(int i=1,x,y,z;i<=m;i++){
        x=read(),y=read(),z=read();
        G[x][y]=G[y][x]=min(G[x][y],z);
    }
    memcpy(d,G,sizeof(G));
}

void get_path(int i,int j){
    if(!mid[i][j])return;
    get_path(i,mid[i][j]);
    p.push_back(mid[i][j]);
    get_path(mid[i][j],j);
}

void solve(){
    for(int k=1;k<=n;k++){
        for(int i=1;i<k;i++)
            for(int j=i+1;j<k;j++)
                if((long long)d[i][j]+G[i][k]+G[k][j]<ans){
                    ans=d[i][j]+G[i][k]+G[k][j];
                    p.clear();
                    p.push_back(i);get_path(i,j);
                    p.push_back(j),p.push_back(k);
                }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)if(d[i][j]>d[i][k]+d[k][j]){
                d[i][j]=d[i][k]+d[k][j];
                mid[i][j]=k;
            }
    }
    if(ans==inf)puts("No solution.");
    else for(int i=0;i<p.size();i++)printf("%d%c",p[i],i==p.size()-1?‘\n‘:‘ ‘);
}

int main(){
    read_and_parse();
    solve();
    return 0;
}

原文地址:https://www.cnblogs.com/wzj-xhjbk/p/9976849.html

时间: 2024-10-13 04:43:08

【POJ1734】Sightseeing Trip 无向图最小环的相关文章

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求最小环)

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: 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: 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

poj1734 Sightseeing trip

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

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

URAL1004 Sightseeing Trip Floyd 最小环

题意:求有权无向图的最小环,环至少包括三个点. 思路: 设map[i,j]表示i到j的的距离.输入有重边,在处理输入的时候只保存最短边. 取环中一个点k,左右点是ij则map[i,k]和map[k,j]是固定的不能变,可改变的是没有加入k点的i,j之间的最短路,设为dist[i,j].那么最短环的长度表示为dist[i,j]+map[i,k]+map[k,j]. Floyd的最外层循环为k时,最短路还没有用k和更新ij之间的最短路,恰好符合要求.所以在还没有用k更新ij之间的最短路之前更新环,每

1999 Central European Olympiad in Informatics - Sightseeing Trip

算法提示 最小环问题 题目大意 在一张带权无向图上,找出至少含 3 个点且权值和最小的环,并按环上的循序输出环上的点.存在重边,无自环. 做法分析 参考最小环问题,在更新 dist[i][j] 时,记录更新其的点 k,便于回溯路径. 参考代码 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <queue> 5 #include <algorithm> 6

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