1004. Sightseeing Trip 最小环

http://acm.timus.ru/problem.aspx?space=1&num=1004
Sightseeing trip 观光旅游
Background

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 necessary to find the shortest route which begins and ends at the same place.
在桑给巴尔岛的Adelton城镇上有一个旅游机构。它们决定在提供许多的其它吸引之外,再向客人们提供旅游本镇的服务。 为了从提供的吸引服务中尽可能地获利,这个旅游机构接收了一个精明决定:在相同的起点与终点之间找出一最短路线。

Problem

Your task is to write a program which finds such a route. In the town there are N crossing points numbered from 1 to N and M two-way roads numbered from 1 to M. Two crossing points can be connected by multiple roads, but no road connects a crossing point with itself. Each sightseeing route is a sequence of road numbers y_1, …, y_k, k>2. The road y_i (1<=i<=k-1) connects crossing points x_i and x_{i+1}, the road y_k connects crossing points x_k and x_1. All the numbers x_1,…,x_k should be different. The length of the sightseeing route is the sum of the lengths of all roads on the sightseeing route, i.e. L(y_1)+L(y_2)+…+L(y_k) where L(y_i) is the length of the road y_i (1<=i<=k). Your program has to find such a sightseeing route, the length of which is minimal, or to specify that it is not possible, because there is no sightseeing route in the town.
你的任务是编写一条程序来找类似的的一条路线。在这个镇上,有N个十字路口(编号1至N),两个十字路口之间可以有多条道路连接,有M条道路(编号为1至M)。但没有一条道路从一个十字路口出发又回到同一个路口。每一条观光路线都是由一 些路组成的,这些道路序号是:y_1, …, y_k,且k>2。第y_i(1<=i<=k-1)号路是连接第x_i号十字路口和第x_{i+1}号十字路 口的;其中第y_k号路是连接第x_k号十字路口和第x_1号十字路口。而且所有的这些x_1,…,x_k分别代表不同路口的序号。在 某一条观光路线上所有道路的长度的和就是这条观光路线的总长度。换言之L(y_1)+L(y_2)+…+L(y_k) 的和 L(y_i)就是第y_i 号观光路线的长度。你的程序必须找出类似的一条路线:长度必须最小,或者说明在这个城镇上不存在这条观光路线。

Input

Input contains a series of tests. The first line of each test contains two positive integers: the number of crossing points N<=100 and the number of roads M<=10000. Each of the next M lines describes one road. It contains 3 positive integers: the number of its first crossing point, the number of the second one, and the length of the road (a positive integer less than 500). Input is ended with a ‘-1’ line.
含几组测试数据。每一组数据的第一行包含两个正整数:十字路口的个数N(N<=100),另一个是道路的 数目M(M<10000)。接下来的每一行描述一条路:每一行有三个正整数:这条路连接的两个路口的编号,以及这条路的长度(小于500的正整数)。最后以-1结束。

Output

Each line of output is an answer. It contains either a string `No solution.’ in case there isn’t any sightseeing route, or it contains the numbers of all crossing points on the shortest sightseeing route in the order how to pass them (i.e. the numbers x_1 to x_k from our definition of a sightseeing route), separated by single spaces. If there are multiple sightseeing routes of the minimal length, you can output any one of them.
每一行输出都是一个答案。如果这条观光路线是不存在的话就显示“No solution.”;或者输出这条最短路线的经过所有十字 路口的序号,每一个序号之间用一个空格符隔开,这样更能说明这条路线是如何得出来的。也就是用从x_1到x_k描述一条观光路线。假如满足最短长度的观光路线不止一条,输出其中的任一条即可。

Sample Input

5 7
1 4 1
1 3 300
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20
4 3
1 2 10
1 3 20
1 4 30
-1
Sample Output

1 3 5 2
No solution.

题解
之前没做过最小环问题
用floyd解决,细节看程序把

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

using namespace std;
const int INF=2100000000/3, maxn=110;
int g[maxn][maxn], d[maxn][maxn], path[maxn][maxn], out[110];
int cnt;
void find_path(int r, int c){
    if (path[r][c]){
        find_path(r, path[r][c]);
        out[cnt++]=path[r][c];
        find_path(path[r][c], c);
    }
}

int main()
{
    int n, m;
    while (scanf("%d", &n)!=EOF && n!=-1){
        scanf("%d", &m);
        for (int i=1; i<=n; i++)
            for (int j=1; j<=n; j++){
                d[i][j]=g[i][j]=INF;
                path[i][j]=0;
            }
        for (int i=0; i<m; i++){
            int u, v, dd;
            scanf("%d%d%d", &u, &v, &dd);
            g[u][v]=g[v][u]=min(g[u][v], dd);
            d[u][v]=d[v][u]=g[u][v];
        }
        int res=INF;
        cnt=0;
        for (int k=1; k<=n; k++)
        {
            for (int i=1; i<=n; i++)
                for (int j=1; j<=n; j++)
                    if (i!=j && j!=k && k!=i && g[i][k]+g[k][j]+d[j][i]<res){
                        res=g[i][k]+g[k][j]+d[j][i];
                        cnt=0;
                        out[cnt++]=i;
                        out[cnt++]=k;
                        out[cnt++]=j;   
                        find_path(j, i);
                    }
            for (int i=1; i<=n; i++)
                for (int j=1; j<=n; j++)
                    if (i!=j && d[i][j]>d[i][k]+d[k][j]){
                        d[i][j]=d[i][k]+d[k][j];
                        path[i][j]=k;
                    }
        }
        if (res==INF) puts("No solution.");else{
            printf("%d", out[0]);
            for (int i=1; i<cnt; i++)
                printf(" %d", out[i]);
            puts("");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值