您的位置:首页 > 其它

CSUOJ 1651 Weirdo

2015-06-02 22:37 344 查看

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 = 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: