poj1734

链接:点击打开链接

题意:从中找出一个最短成环的路线,输出这个路线

代码:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#define maxx 99999999
using namespace std;
int n,m,t;
int dis[105][105],w[105][105],pre[105][105],path[105];
int floyd(){
    int i,j,k,p,x;
    x=maxx;
    for(k=1;k<=n;k++){
        for(i=1;i<k;i++)
        for(j=i+1;j<k;j++)
        if(x>dis[i][j]+w[j][k]+w[k][i]){
            x=dis[i][j]+w[j][k]+w[k][i];
            p=j;
            t=0;
            while(p!=i){                        //用path数组记录路径,pre数组记录j的前一个是什么
                path[t++]=p;
                p=pre[i][p];
            }
            path[t++]=i;
            path[t++]=k;                        //最后不要忘记j的前面还有i和k
        }
        for(i=1;i<=n;i++)
        for(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];                    //更新j的pre数组值
        }                                       //floyd算法模板
    }
    if(x<maxx)
    return 1;
    else
    return 0;                                   //如果没有返回0
}
int main(){
    int i,j,a,b,c,temp;
    while(scanf("%d%d",&n,&m)!=EOF){
        for(i=1;i<=n;i++)
        for(j=1;j<=n;j++){
            dis[i][j]=w[i][j]=maxx;
            pre[i][j]=i;                        //初始化pre数组
        }
        for(i=1;i<=m;i++){
            scanf("%d%d%d",&a,&b,&c);
            if(c<dis[a][b]){                    //要考虑重边
                dis[a][b]=dis[b][a]=c;
                w[a][b]=w[b][a]=c;
            }
        }
        temp=floyd();
        if(temp){
            for(i=0;i<t;i++)
            printf("%d ",path[i]);
            printf("\n");
        }
        else
        printf("No solution.\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-19 06:38:51

poj1734的相关文章

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

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

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

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6647   Accepted: 2538   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题解

题目大意 求一个无向图的最小环 题解 如果是有向图的话,只需要令f[i][i]=+∞,再floyd即可: 对无向图,应该在floyd算法循环至k的一开始进行如下操作: 枚举i和j,如果点i存在经过点j的环,则用i→k,k→j,j→编号小于k的结点→i 的最短路去更新最小环的长度, 即ans=min{ans,map[i][k]+map[k][j]+f[i][j]} 然后更新最小环. 这个工作进行完之后,才可以用floyd计算i→k→j的最短路. Code #include <cstdio> #i

poj1734(floyd求最小环)

Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7483   Accepted: 2827   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 无向图最小环

题目大意:给定一个 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)\

【POJ1734】Sightseeing trip

题目链接:https://www.acwing.com/problem/content/346/ 题目大意:确定无向带权图上至少包含 3 个节点的最小环 solution 一道无向图上的最小环问题 , 考虑 \(Floyd\) , 设 \(i\) 到 \(j\) 间的道路长为 \(f[i][j]\) , 最短路径长 \(g[i][j]\) , 若每次 \(Floyd\) 最外层 遍历到 \(k\) 时 , \(g[i][j]\) 决定了 \(i\) 到 \(j\) 间经过节点编号不超过 \(k\