• 并查集之 A Walk Through the Forest


    A Walk Through the Forest

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3047    Accepted Submission(s): 1114


    Problem Description
    Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
    The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.
     
    Input
    Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.
     
    Output
    For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647
     
    Sample Input
    5 6 1 3 2 1 4 2 3 4 3 1 5 12 4 2 34 5 2 24 7 8 1 3 1 1 4 1 3 7 1 7 4 1 7 5 1 6 7 1 5 2 1 6 2 1 0
     
    Sample Output
    2 4
     
    Source
     
    Recommend
    Eddy
     
     
    其实这题不算并查集 应该算在最短路里面  这题最短里加记忆化DFS  我也学了一下记忆化搜索和最短路 应该说这道题目对我来说是一个新的进步吧
    继续好好加油  虽然是昨天做的题目 但是在今天贴出来 也没什么关系啦 呵呵
    这道题抄袭了一下彪哥的DFS  值得借鉴 呵呵
    View Code
     1 #include <iostream>
     2 //#include <string>
     3 using namespace  std;
     4 #define Max 1002
     5 const int  MAXINT=1<<29;
     6 
     7 int a[Max][Max];
     8 int dist[Max];
     9 int path[Max];
    10 int S[Max],v,n;
    11 
    12 void dijkstra()
    13 {
    14     for ( int i = 1; i <= n; i++) 
    15     {                
    16         dist[i] = a[v][i];     //distÊý×é³õʼ»¯
    17         S[i] = 0;                    
    18     }    
    19     S[v] = 1;   dist[v] = 0;    //¶¥µãv¼ÓÈ붥µã¼¯ºÏ
    20     //Ñ¡Ôñµ±Ç°²»ÔÚ¼¯ºÏSÖоßÓÐ×î¶Ì·¾¶µÄ¶¥µãu
    21     for ( i = 1; i <=n-1; i++ )
    22     {            
    23         int min = MAXINT;  int u = v;
    24         for ( int j=1; j <=n; j++ )        
    25             if ( !S[j] && dist[j] < min )    min = dist[u=j]; 
    26             S[u] = 1;                    //½«¶¥µãu¼ÓÈ뼯ºÏS
    27             for ( int w = 1; w <= n; w++ )    //ÐÞ¸Ä 
    28             {                
    29                 if ( !S[w] && a[u][w] < MAXINT &&dist[u] + a[u][w] < dist[w] )
    30                 {
    31                     dist[w] = dist[u] +a[u][w]; 
    32                 } 
    33             //    cout<<w<<"   "<<dist[w]<<endl;
    34             }
    35     }
    36 }
    37 int roat[Max];
    38 int dfs(int i)
    39 {
    40     int j,ans=0;
    41     if(roat[i]!=0) return roat[i];
    42     if(i==2) return 1;
    43     for(j=1;j<=n;j++)
    44     {
    45         //cout<<i<<"    "<<j<<"   "<<dist[i]<<"    "<<dist[j]<<endl;
    46         if(a[i][j]!=MAXINT&&dist[i]>dist[j])
    47     {
    48         roat[j]=dfs(j);
    49         ans+=dfs(j);
    50     }
    51     }
    52     return roat[i]=ans;
    53 }
    54 
    55 int main()
    56 {
    57     int m;
    58     int b,c,d,i,j;
    59     while (cin>>n,n)
    60     {         
    61         for (i=1;i<=n;i++)
    62         {
    63             for (j=1;j<=n;j++)
    64             {
    65                 a[i][j]=MAXINT;
    66             }
    67         }
    68         cin>>m;
    69         while (m--)
    70         {
    71             cin>>b>>c>>d;
    72             a[b][c]=a[c][b]=d;
    73         }
    74          v=2; //ÖÕµã
    75          dijkstra();
    76         //  cout<<dist[1]<<endl;
    77          memset(roat,0,sizeof(roat));
    78          dfs(1);
    79          cout<<roat[1]<<endl;
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    java DMO及增删改查代码的自动生成
    如果有一天苹果免费了,支付宝怎么办
    jquery实现页面交互的几个小例子
    android中的所谓观察者模式
    随笔——runnable勘误以及其他
    android 源码角度全方位理解filter
    android 你的onfocuschangelistener和android:state_hovered为何不起作用
    android 如何阻断seekbar的触摸事件
    java 传值和传引用
    Android内存优化(使用SparseArray和ArrayMap代替HashMap)
  • 原文地址:https://www.cnblogs.com/wujianwei/p/2588129.html
Copyright © 2020-2023  润新知