1227. Rally Championship

1227. Rally Championship

Time limit: 1.0 second Memory limit: 64 MB
A high-level international rally championship is about to be held. The rules of the race state that the race is held on ordinary roads and the route has a fixed length. You are given a map of the cities and two-way roads connecting it. To make the race safer it is held on one-way roads. The race may start and finish anyplace on the road. Determine if it is possible to make a route having a given length   S.

Input

The first line of the input contains the number of cities   M, the number of roads   N  and the length   S  of the route. 1 ≤   M  ≤ 100; 1 ≤   N  ≤ 10000; 1 ≤   S  ≤ 10 6.   S  is integer.
The following   N  lines describe the roads as triples of integers:   P, Q, R. Here   P  and   Q  are cities connected with a road, and   R  is the length of this road. All numbers satisfy the following restrictions: 1 ≤   P,   Q    M; 1 ≤   R  ≤ 32000.

Output

Write YES to the output if it is possible to make a required route and NO otherwise. Note that answer must be written in capital Latin letters.

Samples

input output
3 2 20
1 2 10
2 3 5
NO
3 3 1000
1 2 1
2 3 1
1 3 1
YES
Problem Source: 2002-2003 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 2002
***************************************************************************************
Determining whether there is a ring, while the length of the path seeking path without recording, can be directly deep search, the condition returns
***************************************************************************************
. 1 #include <the iostream>
 2 #include <cstdio>
 . 3 #include <CString>
 . 4 #include < String >
 . 5 #include <Vector>
 . 6 #include <algorithm>
 . 7 #include <Stack>
 . 8  the using  namespace STD;
 . 9  Long Map [ 1001 ] [ 1001 ]; // adjacency matrix FIG memory 
10  BOOL   VIS [ 1001 ] = { to false }; // tag array 
. 11  BOOL   GS;
 12 is  int n-, m, P, Q, I, J, K, Start;
 13 Long ANS, S, R & lt;
 14  void DFS ( int X, Long ST) // deep search, it is determined whether there is a ring, st length of 
15     {
 16       ANS = ST;
 . 17       IF (ANS> = S) // Path> = s satisfy the return condition 
18 is       {
 . 19           GS = to true ;
 20 is           return ;
 21 is       }
 22 is  
23 is       for ( int  IS = . 1 ; IS <= m; IS ++ )
 24        {
 25            IF(!vis[is]&&map[x][is])
26            {
27                vis[is]=true;
28                long  temp=map[x][is];
29                map[x][is]=map[is][x]=0;
30                if(is==start)//存在环返回
31                 {
32                     gs=true;
33                     return;
34                 }
35             dfs(IS , ST + TEMP);
 36              Map [X] [ IS ] = Map [ IS ] [X] = TEMP; // restore 
37 [              VIS [ IS ] = to false ;
 38 is              IF (GS == to true ) // for insurance, may superfluous 
39               return ;
 40  
41 is             }
 42 is        }
 43 is  }
 44 is   int main ()
 45  {
 46 is       CIN >> >> m >> n- S;
 47       Memset (VIS, to false , the sizeof(vis));
48      memset(map,0,sizeof(map));
49      gs=false;
50      for(i=1;i<=n;i++)
51       {
52           cin>>p>>q>>r;
53           if(map[p][q]==0)
54            map[p][q]=map[q][p]=r;
55           else
56             gs=true;
57       }
58       if(gs==true)
59        {
60            cout<<"YES"<<endl;
61            return 0;
62        }
63        int max1=-1;
64       for(i=1;i<=m;i++)
65        if(!vis[i])
66          {
67              start=i;
68 
69              dfs(i,0);
70              memset(vis,false,sizeof(vis));
71              if(gs==true)
72               {
73                   cout<<"YES"<<endl;
74                   return 0;
75               }
76          }
77           cout<<"NO"<<endl;
78         return 0;
79 
80 
81 
82  }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3256770.html

Guess you like

Origin blog.csdn.net/weixin_34337265/article/details/93448828