zoukankan      html  css  js  c++  java
  • Ural 1004 Sightseeing Trip

    Sightseeing Trip

    Time Limit: 2000ms
    Memory Limit: 16384KB
    This problem will be judged on Ural. Original ID: 1004
    64-bit integer IO format: %lld      Java class name: (Any)
     
    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 y1, …, ykk > 2. The road yi(1 ≤ i ≤ k − 1) connects crossing points xi and xi+1, the road yk connects crossing points xk and x1. All the numbers x1, …, xk 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(y1) + L(y2) + … + L(yk) where L(yi) is the length of the road yi (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

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

    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 x1 to xk 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
    4 3
    1 2 10
    1 3 20
    1 4 30
    -1
    

    Sample Output

    1 3 5 2
    No solution.
    

    Source

     
    解题:Floyd 求最小环
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int INF = 0x3f3f3f3f;
     4 const int maxn = 110;
     5 int n,m,d[maxn][maxn],w[maxn][maxn],fa[maxn][maxn];
     6 vector<int>cycle;
     7 int Floyd() {
     8     int minCycle = INF;
     9     for(int k = 1; k <= n; ++k) {
    10         for(int i = 1; i < k; ++i)
    11             for(int j = i + 1; j < k && w[i][k] < INF; ++j) {
    12                 int tmp = d[i][j] + w[i][k] + w[k][j];
    13                 if(tmp < minCycle) {
    14                     minCycle = tmp;
    15                     cycle.clear();
    16                     int p = j;
    17                     while(p != i) {
    18                         cycle.push_back(p);
    19                         p = fa[i][p];
    20                     }
    21                     cycle.push_back(i);
    22                     cycle.push_back(k);
    23                 }
    24             }
    25         for(int i = 1; i <= n; ++i)
    26             for(int j = 1; j <= n && d[i][k] < INF; ++j) {
    27                 int tmp = d[i][k] + d[k][j];
    28                 if(tmp < d[i][j]) {
    29                     d[i][j] = tmp;
    30                     fa[i][j] = fa[k][j];
    31                 }
    32             }
    33     }
    34     return minCycle;
    35 }
    36 int main() {
    37     int u,v,ww;
    38     while(~scanf("%d",&n)) {
    39         if(n == -1) return 0;
    40         scanf("%d",&m);
    41         for(int i = 0; i < maxn; ++i)
    42             for(int j = 0; j < maxn; ++j) {
    43                 d[i][j] = w[i][j] = INF;
    44                 fa[i][j] = i;
    45             }
    46         while(m--) {
    47             scanf("%d%d%d",&u,&v,&ww);
    48             ww = min(ww,w[u][v]);
    49             w[u][v] = w[v][u] = d[u][v] = d[v][u] = ww;
    50         }
    51         if(Floyd() == INF) puts("No solution.");
    52         else {
    53             printf("%d",cycle[0]);
    54             for(int i = 1; i < cycle.size(); ++i)
    55                 printf(" %d",cycle[i]);
    56             putchar('
    ');
    57         }
    58     }
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    HTML特殊字符编码对照表
    前端研究CSS之文字与特殊符号元素结合的浏览器兼容性总结
    前端研究CSS之内联元素块级化/区域大小/文字和图标的位置
    鼠标滚动事件onscroll在firefox/chrome/Ie中执行次数的问题处理
    Js把对象数组列表转换成数组
    Windows 10简体中文最新预览版Build 9926
    常州陪购师一小时收费300元 1年穿坏20双高跟鞋
    [2015.01.23]办公工具类....不定时添加
    [2015.01.21]数据恢复类....不定时添加
    2015年高速免费通行时间确定 共计免费20天
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4714742.html
Copyright © 2011-2022 走看看