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. DotNetBar v12.7.0.2 Fully Cracked

    更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.7.0.2 如果遇到破解问题可以与我 ...

  2. c++形参和实参同名时,如何单步执行观察形参的变化。

    c++形参和实参同名时,如何单步执行观察形参的变化? 方法:当程序运行到函数中时,添加变量观察即可.

  3. 转:115个Java面试题和答案——终极列表(上)

    转自:http://www.importnew.com/10980.html 本文我们将要讨论Java面试中的各种不同类型的面试题,它们可以让雇主测试应聘者的Java和通用的面向对象编程的能力.下面的 ...

  4. poj2955 区间dp

    //Accepted 200 KB 63 ms //区间dp //dp[i][j] 从i位到j位能得到的最大匹配数 //dp[i][j]=max(dp[i+1][j-1] (s[i-1]==s[j-1 ...

  5. Node.js的简介和安装

    一.Node.js的简介和安装 a)       什么是Node.js? Node.js是一个开发平台 让JavaScript运行在服务器端的开发平台 ---简单点说就是用JavaScript写服务器 ...

  6. javascript笔记3-面向对象的程序设计-创建对象

    javascript的对象:无序属性的集合,而每个属性可以包含基本值.对象或者函数.举例: var person = new Object(); person.name = "Nichola ...

  7. php大力力 [013节]mySQL数据库乱码问题我还没解决

    <?php echo"测试<br>"; $sql_connection = mysql_connect("localhost","e ...

  8. LeetCode----Copy List with Random Pointer 深度拷贝,浅度拷贝,Lazy拷贝解析

    题目:A linked list is given such that each node contains an additional random pointer which could poin ...

  9. HTML的表单

    HTML表单 <!-- <form></form>标签对用来创建一个表单,即定义表单的开始和结束位置,<form>表单具有下面等属性 1.action属性用来 ...

  10. 作业1-我的第一个博客&GuiHub简单练习

    自我介绍                   姓名:苗中峰(不要问有什么含义,翻着字典取的名,翻到什么字就取了什么字)                               性别:男(不解释) ...