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 integers M, N and S that are the number of cities, the number of roads the length of the route (1 ≤ M ≤ 100; 1 ≤ N ≤ 10 000; 1 ≤ S ≤ 2 · 106).
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, QM; 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,

【分析】先判断是否有环,如果有则YES,没有的话找最大直径,如果大于等于s,则YES,其他情况则NO。注意有图不连通情况。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int n,m,cnt=;
int tot=,s,t,son,sum;
int head[N],dis[N],vis[N],pre[N],vis1[N];
int w[N][N];
int in[N],out[N];
int bfs(int x) {
met(vis,);
met(dis,);
sum=;
queue<int>Q;
Q.push(x);
vis[x]=;vis1[x]=;
while(!Q.empty()) {
int t=Q.front();
Q.pop();//printf("t=%d\n",t);
bool has=false;
for(int i=; i<=n; i++) {
if(!vis[i]&&w[t][i]!=) {
Q.push(i);
dis[i]=dis[t]+w[t][i];
vis[i]=vis1[i]=;
has=true;
}
}
if(!has) {
if(dis[t]>sum) {
sum=dis[t];
//printf("%d %d\n",sum,dis[t]);
son=t;
}
}
}
return sum;
}
int main() {
int u,v,l,sum=;
scanf("%d%d%d",&n,&m,&s);
while(m--) {
scanf("%d%d%d",&u,&v,&l);
w[u][v]=w[v][u]=l;
in[u]++;
in[v]++;
}
queue<int>q;
for(int i=; i<=n; i++) {
if(in[i]<=)q.push(i);
}
while(!q.empty()) {
int t=q.front();
q.pop();
vis[t]=;
for(int i=; i<=n; i++) {
if(!vis[i]&&w[t][i]!=) {
in[i]--;
if(in[i]==)q.push(i);
}
}
}
bool flag=false;
for(int i=; i<=n; i++) {
if(!vis[i])flag=true;
}
if(flag)printf("YES\n");
else {
int ans=-;
memset(vis1,,sizeof vis1);
for(int i=; i<=n; i++)
if(!vis1[i]) {
bfs(i);
ans=max(ans,bfs(son));
}
if(ans>=s)puts("YES");
else puts("NO");
}
return ;
}

最新文章

  1. PS中的图像知识
  2. virtualbox 下windows与虚拟机实现文件共享---挂载
  3. CSS3绘制弹球动画效果
  4. compareTo(String str)与compareToIgnoreCase(String str)
  5. 【freemaker】之include,import区别
  6. Sql中的Merge和output
  7. Python之Python Imaging Library
  8. initial,常用于消除css格式
  9. android软件开发之webView.addJavascriptInterface循环渐进【一】
  10. 初次运行 Git 前的配置
  11. Openjudge-NOI题库-数根
  12. Bootstrap入门(十二)组件6:导航标签页
  13. Java开发之Java对数组的复制
  14. 微信小程序开发心得--动画机制
  15. Eclipse中的所有快捷键列表
  16. Tihinkphp3.2整合最新版阿里大鱼进行短信验证码发送
  17. selenium截图
  18. Highcharts绘制曲线图小结
  19. 编译安装redis4.0
  20. 获取relatedTarget属性

热门文章

  1. Java与.NET DES加密解密互转
  2. C++全局变量的声明和定义
  3. asp.net 代码 注意点
  4. ssh原理
  5. C#判断IP地址是否合法函数-使用正则表达式-2个 (转)
  6. HTML---6 运算符,类型转换
  7. iOS定时器NSTimer的使用方法
  8. C#注册表常用操作
  9. 如何由Height Map生成Normal Map
  10. C++ inline weak symbol and so on