zoukankan      html  css  js  c++  java
  • POJ1734

    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

    题目大意:给你一个无向图,要你求最小环,并输出路径

    用floyd求最短路时顺便求最小环

    floyd主程序

    1 for k:=1 to n do
    2   for i:=1 to n do
    3     for j:=1 to n do
    4       f[i,j]:=min(f[i,j],f[i,k]+f[k,j]);

    然后我们可以在里面加一点东西

    1 for k:=1 to n do
    2   begin
    3     for i:=1 to k-1 do
    4       for j:=1 to i-1 do
    5         minc:=min(minc,f[i,j]+g[i,k]+g[k,j]);
    6     for i:=1 to n do
    7       for j:=1 to n do
    8         f[i,j]:=min(f[i,j],f[i,k]+f[k,j]);
    9   end;

    g存的是原图信息

    因为当k枚举到a时,最短路除了两端点外,都只能经过编号小于a的点

    在最小环中,一定有一个编号最大的点,而且只有一个(废话......)

    设这个点编号为b,当k枚举到b时,i,j枚举到b在环上相邻的两点时,f[i,j]存的是i,j之间不通过大于b的点的最短路,这当然就是最小环了

     1 const
     2     maxn=102;
     3 var
     4     f,g,p:array[0..maxn,0..maxn]of longint;
     5     path:array[0..maxn]of longint;
     6     ans,tot,n,m:longint;
     7 
     8 procedure init;
     9 var
    10     i,x,y,z:longint;
    11 begin
    12     read(n,m);
    13     fillchar(g,sizeof(g),1);
    14     for i:=1 to m do
    15       begin
    16         read(x,y,z);
    17         if g[x,y]>z then
    18         begin
    19           g[x,y]:=z;
    20           g[y,x]:=z;
    21         end;
    22       end;
    23     f:=g;
    24 end;
    25 
    26 procedure get(i,j:longint);
    27 begin
    28     if p[i,j]<>0 then
    29     begin
    30       get(i,p[i,j]);
    31       get(p[i,j],j);
    32       exit;
    33     end;
    34     inc(tot);
    35     path[tot]:=j;
    36 end;
    37 
    38 procedure work;
    39 var
    40     i,j,k:longint;
    41 begin
    42     ans:=g[0,0];
    43     for k:=1 to n do
    44       begin
    45         for i:=1 to k-1 do
    46           for j:=1 to i-1 do
    47             if ans>f[i,j]+g[i,k]+g[k,j] then
    48             begin
    49               ans:=f[i,j]+g[i,k]+g[k,j];
    50               tot:=0;
    51               inc(tot);
    52               path[tot]:=i;
    53               get(i,j);
    54               inc(tot);
    55               path[tot]:=k;
    56             end;
    57         for i:=1 to n do
    58           for j:=1 to n do
    59             if f[i,j]>f[i,k]+f[k,j] then
    60             begin
    61               p[i,j]:=k;
    62               f[i,j]:=f[i,k]+f[k,j];
    63             end;
    64       end;
    65     if ans=g[0,0] then write('No solution.')
    66     else
    67       for i:=1 to tot do
    68         write(path[i],' ');
    69 end;
    70 
    71 begin
    72     init;
    73     work;
    74 end.
    View Code
  • 相关阅读:
    个人作业3——个人总结(Alpha阶段)
    结对编程2 单元测试
    英语学习app案例分析
    结对作业1
    java四则运算生成器
    个人附加作业 201421123108 王坤彬 网络1414
    个人作业3-(Alpha阶段)
    结对编程之单元测试 201421123108 王坤彬
    英语学习案例分析APP 201421123108 王坤彬
    结对编程 王坤彬 201421123108
  • 原文地址:https://www.cnblogs.com/Randolph87/p/3639136.html
Copyright © 2011-2022 走看看