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 ;
}

URAL 1227 Rally Championship(树的直径)(无向图判环)的更多相关文章

  1. 1227. Rally Championship

    1227 题意木看懂 是可以停在路上 任何地方 水题一枚 以下条件之一满足就可以 有环(并查集判) 重边 自己到自己的边 最长边大于s(用flod改写下) #include <iostream& ...

  2. BZOJ 1854: [Scoi2010]游戏 无向图判环

    题目链接: 题目 1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB 问题描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装 ...

  3. 与图论的邂逅01:树的直径&基环树&单调队列

    树的直径 定义:树中最远的两个节点之间的距离被称为树的直径.  怎么求呢?有两种官方的算法(不要问官方指谁我也不晓得): 1.两次搜索.首先任选一个点,从它开始搜索,找到离它最远的节点x.然后从x开始 ...

  4. hdu-4612(无向图缩点+树的直径)

    题意:给你n个点和m条边的无向图,问你如果多加一条边的话,那么这个图最少的桥是什么 解题思路:无向图缩点和树的直径,用并查集缩点: #include<iostream> #include& ...

  5. HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  6. URAL 1145—— Rope in the Labyrinth——————【求树的直径】

    Rope in the Labyrinth Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64 ...

  7. hdu4612 无向图中随意加入一条边后使桥的数量最少 / 无向图缩点+求树的直径

    题意如上,含有重边(重边的话,俩个点就能够构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选随意起点U,进行bfs,到达最远的一个点v ...

  8. hdu4612 无向图中任意添加一条边后使桥的数量最少 / 无向图缩点+求树的直径

    题意如上,含有重边(重边的话,俩个点就可以构成了边双连通). 先缩点成树,在求数的直径,最远的连起来,剩下边(桥)的自然最少.这里学习了树的直径求法:第一次选任意起点U,进行bfs,到达最远的一个点v ...

  9. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

随机推荐

  1. POJ 3525 半平面交+二分

    二分所能形成圆的最大距离,然后将每一条边都向内推进这个距离,最后所有边组合在一起判断时候存在内部点 #include <cstdio> #include <cstring> # ...

  2. php变量的判空和类型判断

    (1)var_dump(); 判断一个变量是否已经声明并且赋值,并且打印类型和值 <?php $a; var_dump($a);//输出null <?php var_dump($a);// ...

  3. Ad Muncher 宣布免费

    Windows平台广告过滤软件Ad Muncher宣布免费 详见:http://www.admuncher.com/free 下载:http://www.admuncher.com/static/fi ...

  4. 记录一些容易忘记的属性 -- UIKeyboard

    //UIKeyboardWillShowNotification这个通知在软键盘弹出时由系统发送    //UIKeyboardWillShowNotification 通知:键盘将要显示的通知    ...

  5. java基础-005

    27.Java中垃圾回收的目的及回收的时机 垃圾回收的目的是识别并且丢弃不再使用的对象来释放和重用资源. 如果对象的引用被置为null,垃圾收集器不会立即释放对象占用的内存. 什么时候进行垃圾回收,主 ...

  6. MongoDB C#驱动中Query几个方法 (转)

    Query.All("name", "a", "b");//通过多个元素来匹配数组 Query.And(Query.EQ("nam ...

  7. 有关PHP安装,基础学习

    首先要安装 wamp 和 NavicatMySQLFront (要在非中文目录下) 打开DW 点击站点 ——新建站点:设置站点名称,选择本地站点文件夹:wap\www 服务器:添加 +    服务器名 ...

  8. STM32下载显示target dll has been cancelled

    使用MDK 4.74向STM32下载时出现各种错误,而且时隐时现, Internal command error.Error:Flash download failed. Target DLL has ...

  9. Objective-C中NSValue的使用

    我们在C/C++开发中常会用到结构体来帮助我们简单封装基本数据类型,在Objective-C中我们也可以使用结构体来完成数据类型的封装.同时,Cocoa Touch还提供了一个NSValue来帮助我们 ...

  10. Matlab单一变量曲线拟合-cftool

    2.启动曲线拟合工具箱>cftool 3.进入曲线拟合工具箱界面“Curve Fitting tool”(1)点击“Data”按钮,弹出“Data”窗口:(2)利用X data和Y data的下 ...