CSUOJ 1651 Weirdo
1651: Weirdo
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 40 Solved: 21
[Submit][Status][Web Board]
Description
小x是一个奇葩,奇葩的小x终于盼来了五一长。。好吧!短假。早已乏味的大学生活让小x感到绝望。与其天天做在电脑面前废寝忘食的打LOL,还不如来一场说走就走的旅行,去看看外面的世界!已经准备去往B市的小x决定让自己的旅行更有意义一点,他觉得人生本就是一场漫长的旅行,重要的不是终点,而是奇葩的路线。他决定找一条通往B市的最均匀的路线!什么样的路线最呀最均匀?当然是这条线路上的路段之间的宽度差的绝对值最小的那条就均匀啦!现在给出n个城市和m条道路及这m条道路的路宽,并且保证居住在A市的小x是可以到达B市,你能帮助小x找出这样的最均匀的路线么,输出这条路线上的最大差值!
Input
每个样例的第一行n,m,A,B分别表示有n个点,m条路段,小x的居住地和小x要到的B市,接下来m行,每行三个数字u,v,w分别表示从u到v的这段路的路宽(路是双向的)。
2 <= n <= 1500,
m <= 3000,
0 <= v,u < n,
w < INT_MAX
Output
每个样例输出一行
Sample Input
5 5 4 0
0 3 22022
1 2 8871
1 3 9421
2 4 24398
3 4 3344
Sample Output
15527
HINT
Source
解题:直接枚举下界,求最小上界用类似于最小生成树Kruskal的做法
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct arc{
int u,v,w;
bool operator<(const arc &t) const{
return w < t.w;
}
}e[maxn];
int uf[maxn],n,m,S,T,ret;
bool flag;
int Find(int x){
int t = x;
while(uf[x] != x) x = uf[x];
while(uf[t] != t){
int tmp = uf[t];
uf[t] = x;
t = tmp;
}
return x;
}
int kruskal(int low){
for(int i = ; i <= n; ++i) uf[i] = i;
for(int i = low; i < m; ++i){
int x = Find(e[i].u);
int y = Find(e[i].v);
if(x == y) continue;
uf[x] = y;
if(Find(S) == Find(T)) return e[i].w - e[low].w;
if((LL)e[low].w + ret <= e[i].w) return INF;
}
flag = false;
return INF;
}
int main(){
//freopen("Weirdo.in","r",stdin);
//freopen("oo.txt","w",stdout);
while(~scanf("%d %d %d %d",&n,&m,&S,&T)){
for(int i = ; i < m; ++i)
scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);
sort(e,e+m);
ret = INF;
flag = true;
for(int i = ; i < m && flag; ++i)
ret = min(kruskal(i),ret);
printf("%d\n",ret);
}
return ;
}
CSUOJ 1651 Weirdo的更多相关文章
- poj 1651 Multiplication Puzzle
题目链接:http://poj.org/problem?id=1651 思路:除了头尾两个数不能取之外,要求把所有的数取完,每取一个数都要花费这个数与相邻两个数乘积的代价,需要这个代价是最小的 用dp ...
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- csuoj 1511: 残缺的棋盘
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec 内存限制: 128 MB 题目描述 输入 ...
- POJ 1651 (区间DP)
题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少 ...
- NOJ 1651 Red packet(二分)
[1651] Red packet 时间限制: 1000 ms 内存限制: 65535 K 问题描述 New Year is coming! Our big boss Wine93 will dist ...
- csuoj 1354: Distinct Subsequences
这个题是计算不同子序列的和: spoj上的那个同名的题是计算不同子序列的个数: 其实都差不多: 计算不同子序列的个数使用dp的思想: 从头往后扫一遍 如果当前的元素在以前没有出现过,那么dp[i]=d ...
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )
线段树.. -------------------------------------------------------------------------------------- #includ ...
- BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 553 ...
- 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 566 Sol ...
随机推荐
- 小贝_mysql select5种子句介绍
mysql select5种子句介绍 简要 一.五种字句 二.具体解释五种字句 一.五种字句 where.group by.having.order by.limit 二.具体解释五种字句 2.1.理 ...
- php 数组元素高速去重
1.使用array_unique方法进行去重 对数组元素进行去重.我们通常会使用array_unique方法,使用这种方法能够把数组中的元素去重. <?php $arr = array(1,1, ...
- JNI之——'cl' 不是内部或外部命令,也不是可执行的程序或批处理文件
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46604315 问题的出现: 今天卸载了VS2010,重装vs2008后.发 ...
- cpc,a wonderful concert
做完这道题突然就感觉自己脑子是不是已经秀逗了,tle到死后才想起来找规律, 就是求排列数的题目,按插入点对状态进行分类,可以暴力tle... #include<iostream> #inc ...
- 曲根英语万词---二、evoke
曲根英语万词---二.evoke 一.总结 一句话总结:evoke v.唤起,引起 词根:-voc-, -vok- [词根含义]:声音,叫喊 1.consecrate? v,供奉,奉为神圣 -ate, ...
- springboot 测试类,项目使用shiro时报错UnavailableSecurityManagerException
大概的问题就是,正常运行项目是没有问题的 使用测试类是,加载不了shiro的securityManager,主要导致不是很清楚,望告知, 解决方法 @Resource org.apache.shiro ...
- codeforces 495D Sonya and Matrix
Since Sonya has just learned the basics of matrices, she decided to play with them a little bit. Son ...
- myisam和innodb的qubie
1.Myisam 支持锁表,innoDB 支持行锁. 2.innoDB 和 BDB 支持事务. 3.Myisam 与 innoDB 索引的区别: Myisam 无论是主键索引还是其他索引,索 ...
- Java 调用对象方法的执行过程
弄清调用对象方法的执行过程十分重要.下面是调用过程的详细描述: 1) 编译器查看对象的声明类型和方法名.假设调用x.f(param),且隐式参数x声明为C类的对象.需要注意的是:有可能存在多个名为f, ...
- PKI 信息安全三大特性
[机密性]发送方 接收方明文 M ...