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

Input

The first line of input 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).

Output

There is only one line in output. 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.

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

Sample Output

1 3 5 2

Source

CEOI 1999

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=105;
const int INF=1e6;//改成1e9就不对了
int dis[MAXN][MAXN],mp[MAXN][MAXN];
int R[MAXN][MAXN],path[MAXN];
int N,M,len;
int u,v,w;
int cnt=0;
void Init()
{
    for(int i=0;i<=N;i++)
        for(int j=0;j<=N;j++)
        dis[i][j]=INF,R[i][j]=0;
    cnt=0;
}
void Path(int s,int t)
{
    if(R[s][t])
    {
        Path(s,R[s][t]);
        Path(R[s][t],t);
    }
    else
        path[++cnt]=t;
}
void Floyd()
{
    len=INF;
    for(int k=1;k<=N;k++)
    {    //判断负环
        for(int i=1;i<k;i++)
            for(int j=i+1;j<k;j++)
            {
                if(len>dis[i][j]+mp[i][k]+mp[k][j])
                {
                    len=dis[i][j]+mp[i][k]+mp[k][j];
                    cnt=0;
                    path[++cnt]=i;
                    Path(i,j);
                    path[++cnt]=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];
                    R[i][j]=k;
                }
            }
    }
}
int main ()
{
    while(~scanf("%d%d",&N,&M))
    {
        Init();
        for(int i=1;i<=M;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(dis[u][v]>w)//记录重边
            {
                dis[u][v]=w;
                dis[v][u]=w;
            }
        }
        for(int i=1;i<=N;i++)
            for(int j=1;j<=N;j++)
                mp[i][j]=dis[i][j];//mp判断环的时候要用到
        Floyd();
        if(len==INF)
            printf("No solution.\n");
        else
        {
            for(int i=1;i<=cnt;i++)
            {
                if(i!=cnt)
                    printf("%d ",path[i]);
                else
                    printf("%d\n",path[i]);
            }
        }
    }
    return 0;
}

  

时间: 2024-11-03 20:52:43

POJ1734/Floyd求最小环的相关文章

HDU - 1599 find the mincost route(Floyd求最小环)

find the mincost route Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个

【BZOJ 1027】 (凸包+floyd求最小环)

[题意] 某公司加工一种由铁.铝.锡组成的合金.他们的工作很简单.首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同.然后,将每种原材料取出一定量,经过融解.混合,得到新的合金.新的合金的铁铝锡比重为用户所需要的比重. 现在,用户给出了n种他们需要的合金,以及每种合金中铁铝锡的比重.公司希望能够订购最少种类的原材料,并且使用这些原材料可以加工出用户需要的所有种类的合金. [分析] 只要考虑前两个物质的比例,因为加起来等于1. 如果你看过zoj3154,就会知道这题的模型,用二元组表

多源最短路径Floyd、Floyd求最小环【模板】

Floyd算法:用来找出每对点之间的最短距离.图可以是无向图,也可以是有向图,边权可为正,也可以为负,唯一要求是不能有负环. 1.初始化:将Map[][]中的数据复制到Dist[][]中作为每对顶点之间的最短路径的初值,Pre[i][j] = i 表示 i 到 j 路径中 j 的前一节点. 2. k 从 1 到 N 循环 N 次,每次循环中,枚举图中不同的两点 i,j,如果Dist[i][j] > Dist[i][k] + Dist[k][j],则更新Dist[i][j] = Dist[i][k

Floyd求最小环!(转载,非原创)

//Floyd 的 改进写法可以解决最小环问题,时间复杂度依然是 O(n^3),储存结构也是邻接矩阵 int mincircle = infinity; Dist = Graph; for(int k=0;k<nVertex;++k){ //新增部分: for(int i=0;i<k;++i) for(int j=0;j<i;++j) mincircle = min(mincircle,Dist[i][j]+Graph[j][k]+Graph[k][i]); //通常的 floyd 部分

HDU 1599 find the mincost route (Floyd求最小环) &gt;&gt;

Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在8600想找一条旅游路线,这个路线从A点出发并且最后回到A点,假设经过的路线为V1,V2,....VK,V1,那么必须满足K>2,就是说至除了出发点以外至少要经过2个其他不同的景区,而且不能重复经过同一个景区.现在8600需要你帮他找一条这样的路线,并且花费越少越好. Input 第一行是2个整数N和M(N <= 100, M <= 1000),代表景区的个数和道路的条数. 接下来的M行里,每行包括

弗洛伊德Floyd求最小环

模板: #include<bits/stdc++.h> using namespace std; const int MAXN = 110; const int INF = 0xffffff0; int temp,Map[MAXN][MAXN],Dist[MAXN][MAXN],pre[MAXN][MAXN],ans[MAXN*3]; void Solve(int i,int j,int k) { temp = 0; //回溯,存储最小环 while(i != j) { ans[temp++]

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

USACO 4.1 Fence Loops(Floyd求最小环)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes more than two fences

Floyd求最小环

首先求最小环有一个比较好想的方法:每次删掉一条边,看看这条边n所连的点i之间的距离(dijkstra),时间复杂度O(m*V^2*logv) 其实floyd也能完成这个功能.f[i][j][k]表示i到j在中间点为1~k的最近距离 对于一个环,我们假设i和j中只夹这一个数k,则环长为f[i][j][k-1]+map[i][k]+map[k][j],枚举一下 #include<iostream> #include<cstdio> #include<algorithm> #