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. hashmap和hashtable,arraylist和vector的区别

    hashmap线程不安全,hashtable线程安全 hashmap允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同. ...

  2. Android 自定义属性

    values新建一个attrs.xml<resource>    <declare-styleable name = "MyTextView">    &l ...

  3. IOS文件管理-NSFileMangager-NSdata

    Ios下的文件管理, Ios下不像windows 文件系统那样可以访问任何的文件目录,如C盘.D盘什么的.在Ios中每个应用程序只能访问当前程序的目录,也即sandbox(沙盒模型). iOS为每个应 ...

  4. 用Quartz进行作业调度(转)

    概述 各种企业应用几乎都会碰到任务调度的需求,就拿论坛来说:每隔半个小时生成精华文章的RSS文件,每天凌晨统计论坛用户的积分排名,每隔30分钟执行锁定用户解锁任务. 对于一个典型的MIS系统来说,在每 ...

  5. 关于dllimport的使用

    最近做一个动态加载插件的项目,插件中的dll 主要是各厂商各型号的读卡器的通用类库,stdapi.dll,WltRS.dll,有的还有进一步封装的dll,主要是为了简化通用类库的操作. 这些类库都是用 ...

  6. .net CHARTING图表控件下载地址

    .net CHARTING是一款功能强大的图表控件,利用.NET framework和GDI+为工作于ASP.NET和Winform的C#和VB.NET开发人员提供可托管的图表解决方案,提供了多种2D ...

  7. 进行以上Java编译的时候,出现unmappable character for encoding GBK。

    public class Exerc02{ public static void main(String args []){ char c = '中国人'; System.out.pingtln(c) ...

  8. error: Apostrophe not preceded by \

    解决方案为:在编译出错提示中找到相关的string.xml文档,在string标签中的字符串含有单引号(')前面,加上反斜杠(\)转义即可.

  9. PHP中的文件下载

    文件下载:用<a href="链接"></a>这种是下载,但对于浏览器能解释的文件类型此下载非彼下载.向服务器请求的时候:1.协议和版本2.头信息3.请求的 ...

  10. 三色二叉树_树形DP

    Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 一棵二叉树可以按照如下规则表示成一个由0.1.2组成的字符序 ...