zoukankan      html  css  js  c++  java
  • USACO Section 3.3 Riding The Fences (fence)

    Riding the Fences

    Farmer John owns a large number of fences that must be repaired annually. He traverses the fences by riding a horse along each and every one of them (and nowhere else) and fixing the broken parts.

    Farmer John is as lazy as the next farmer and hates to ride the same fence twice. Your program must read in a description of a network of fences and tell Farmer John a path to traverse each fence length exactly once, if possible. Farmer J can, if he wishes, start and finish at any fence intersection.

    Every fence connects two fence intersections, which are numbered inclusively from 1 through 500 (though some farms have far fewer than 500 intersections). Any number of fences (>=1) can meet at a fence intersection. It is always possible to ride from any fence to any other fence (i.e., all fences are "connected").

    Your program must output the path of intersections that, if interpreted as a base 500 number, would have the smallest magnitude.

    There will always be at least one solution for each set of input data supplied to your program for testing.

    PROGRAM NAME: fence

    INPUT FORMAT

    Line 1: The number of fences, F (1 <= F <= 1024)
    Line 2..F+1: A pair of integers (1 <= i,j <= 500) that tell which pair of intersections this fence connects.

    SAMPLE INPUT (file fence.in)

    9
    1 2
    2 3
    3 4
    4 2
    4 5
    2 5
    5 6
    5 7
    4 6
    

    OUTPUT FORMAT

    The output consists of F+1 lines, each containing a single integer. Print the number of the starting intersection on the first line, the next intersection's number on the next line, and so on, until the final intersection on the last line. There might be many possible answers to any given input set, but only one is ordered correctly.

    SAMPLE OUTPUT (file fence.out)

    1
    2
    3
    4
    2
    5
    4
    6
    5
    7
    
    思路:dfs+欧拉路(欧拉回路,欧拉通路)的基本定理,不得不说,这题实在是做不下去了,看了别人的代码才过
    Executing...
       Test 1: TEST OK [0.000 secs, 6460 KB]
       Test 2: TEST OK [0.000 secs, 6460 KB]
       Test 3: TEST OK [0.000 secs, 6460 KB]
       Test 4: TEST OK [0.000 secs, 6460 KB]
       Test 5: TEST OK [0.000 secs, 6460 KB]
       Test 6: TEST OK [0.000 secs, 6460 KB]
       Test 7: TEST OK [0.011 secs, 6460 KB]
       Test 8: TEST OK [0.022 secs, 6460 KB]
    
    All tests OK.

     1 /*
     2 ID:wuhuaju2
     3 PROG:fence
     4 LANG:C++
     5 */
     6 #include <cstdio>
     7 #include <iostream>
     8 #include <cstdlib>
     9 #include <algorithm>
    10 #include <cstring>
    11 #include <string>
    12 using namespace std;
    13 
    14 const int maxn=510,maxm=1110;
    15 int sum[10],tot[maxn];
    16 int f[maxn][maxn];
    17 int a[maxn][maxm],b[10],ans[maxm];
    18 int cnt,n,x,y,maxx;
    19 bool vis[maxn];
    20 
    21 void close()
    22 {
    23     fclose(stdin);
    24     fclose(stdout);
    25     exit(0);
    26 }
    27 
    28 void dfs(int k)
    29 {
    30 
    31     for (int i=1;i<=maxn;i++)
    32     {
    33         if (f[k][i]>0)
    34         {
    35             f[k][i]--;
    36             f[i][k]--;
    37             dfs(i);
    38         }
    39     }
    40             cnt++;
    41             ans[cnt]=k;
    42 }
    43 
    44 void work()
    45 {
    46     x=-100;
    47     for (int i=1;i<=maxx;i++)
    48         if (tot[i] % 2==1)
    49         {
    50             x=i;
    51             break;
    52         }
    53     if (x==-100)
    54     {
    55         for (int i=1;i<=maxx;i++)
    56         {
    57             if (tot[i]>0)
    58             {
    59                 x=i;
    60                 break;
    61             }
    62         }
    63     }
    64     dfs(x);
    65     for (int i=1;i<=cnt;i++)
    66         cout<<ans[cnt-i+1]<<endl;
    67 }
    68 
    69 void init ()
    70 {
    71 freopen("fence.in","r",stdin);
    72 freopen("fence.out","w",stdout);
    73     scanf("%d",&n);
    74      memset(tot,0,sizeof(tot));
    75      memset(f,0,sizeof(f));
    76      maxx=0;
    77      for (int i=1;i<=n;i++)
    78      {
    79          scanf("%d %d",&x,&y);
    80          maxx=max(max(maxx,x),y);
    81          f[x][y]++;
    82          f[y][x]++;
    83          tot[x]++;
    84          tot[y]++;
    85     //     vis[x]=true;
    86     //     vis[y]=true;
    87      }
    88 
    89 }
    90 
    91 int main ()
    92 {
    93     init();
    94     work();
    95     close();
    96     return 0;
    97 }
    
    
    
     
  • 相关阅读:
    [WWF]相关资料整理
    [CSharp]经常遇到字符编码转换的问题
    [BizTalk]BizTalk Server 2004的监控和管理
    [Linux]删除目录
    [BizTalk]好用的BizTalk管理工具
    [JWF]使用脚本访问ActiveDirectory(一)ADODB
    [BizTalk][Why]持久化数据库的状态时出现异常
    [BizTalk]关于BizTalk中的命名规则
    FTP连接
    [导入]跟踪BTSWebSvcWiz.exe的执行过程
  • 原文地址:https://www.cnblogs.com/cssystem/p/2910916.html
Copyright © 2011-2022 走看看