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
***************************************************************************************
判断环是否存在,同时求路径的长度  不用记录路径,可直接深搜,满足条件返回
***************************************************************************************
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<vector>
 6 #include<algorithm>
 7 #include<stack>
 8 using namespace std;
 9 long map[1001][1001];//邻接矩阵存图
10 bool  vis[1001]={false};//标记数组
11 bool  gs;
12 int n,m,p,q,i,j,k,start;
13 long ans,s,r;
14 void dfs(int x,long st)//深搜,判断是否存在环,st为长度
15    {
16      ans=st;
17      if(ans>=s)//路径>=s满足条件返回
18      {
19          gs=true;
20          return;
21      }
22 
23      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;//还原
37             vis[is]=false;
38             if(gs==true)//为了保险,可能多此一举
39              return;
40 
41            }
42       }
43  }
44  int main()
45  {
46      cin>>m>>n>>s;
47      memset(vis,false,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

转载于:https://www.cnblogs.com/sdau--codeants/p/3256770.html

猜你喜欢

转载自blog.csdn.net/weixin_34337265/article/details/93448828
今日推荐