CSUOJ 1651 Weirdo
2015-06-02 22:37
344 查看
1651: Weirdo
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 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 = 40001; 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 = 0; 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 = 0; 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 = 0; i < m && flag; ++i) ret = min(kruskal(i),ret); printf("%d\n",ret); } return 0; }
View Code
相关文章推荐
- 《Python基础教程》 读书笔记 第九章 魔法方法、属性和迭代器(上)
- 2015-05月份学习总结 分类: 学习总结 2015-06-02 22:37 82人阅读 评论(0) 收藏
- 2015-05月份学习总结 分类: 学习总结 2015-06-02 22:37 83人阅读 评论(0) 收藏
- 毕业两年何去何从?
- 短期学习计划
- bootstrap结合thinkphp编辑网站
- char、varchar、text和nchar、nvarchar、ntext的区别详解
- LeetCode Sort List
- MJRefresh
- jQuery事件绑定—on()、bind()与delegate()
- ABP启动配置
- DiscreteCosineTransform API 及离散余弦变化
- screen
- linux中的grep命令用法
- hdu 3117(矩阵快速幂)
- android内存泄露检测工具--LeakCanary 中文使用说明
- Junit使用过程中需要注意的诡异bug以及处理办法
- 好友服务器的实现
- 在 slua 中使用更新的面向对象方案
- linux中的grep命令用法