【BZOJ】1050: [HAOI2006]旅行comf(暴力+并查集)

http://www.lydsy.com/JudgeOnline/problem.php?id=1050

表示被暴力吓到了orz

我竟然想不到。。。我竟然还想到分数规划,,但是不可做。。。然后又想到最小生成树,,然后不会做orz

我一直在纠结怎么最大化(或最小化)分母和最小化(或最大化)分子的做法。。。。。但是。。。。不会orz

没想到是暴力orz

直接排序后枚举最小的边,生成树后要最大的边最小(排序后即可orz),然后更新答案即可。

但是不知道之前写错了啥wa了两发,,,随便改改才过orz

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=1005, M=10005;
struct dat { int x, y, w; }a[M];
inline const bool cmp(const dat &a, const dat &b) { return a.w<b.w; }
int p[N], ans1, ans2, n, m;
const int ifind(const int &x) { return x==p[x]?x:p[x]=ifind(p[x]); }
const int gcd(const int &a, const int &b) { return !b?a:gcd(b, a%b); }

int main() {
	read(n); read(m);
	for1(i, 1, m) read(a[i].x), read(a[i].y), read(a[i].w);
	int s=getint(), t=getint();
	sort(a+1, a+1+m, cmp);
	bool flag=1;
	for1(i, 1, m) {
		for1(j, 1, n) p[j]=j;
		int j=i;
		for(; j<=m; ++j) {
			int fx=ifind(a[j].x), fy=ifind(a[j].y);
			p[fx]=fy;
			if(ifind(s)==ifind(t)) {
				flag=0;
				if(a[j].w*ans2<=a[i].w*ans1) ans1=a[j].w, ans2=a[i].w;
				break;
			}
		}
		if(ifind(s)!=ifind(t)) break;
		if(a[j].w*ans2<=a[i].w*ans1) ans1=a[j].w, ans2=a[i].w;
	}
	if(flag) { puts("IMPOSSIBLE"); return 0; }
	int d=gcd(ans1, ans2);
	ans1/=d; ans2/=d;
	if(ans2==1) printf("%d\n", ans1);
	else printf("%d/%d\n", ans1, ans2);
	return 0;
}

  

 


 

 

Description

给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000)。给你两个顶点S和T,求一条路径,使得路径上最大边和最小边的比值最小。如果S和T之间没有路径,输出”IMPOSSIBLE”,否则输出这个比值,如果需要,表示成一个既约分数。 备注: 两个顶点之间可能有多条路径。

Input

第一行包含两个正整数,N和M。 下来的M行每行包含三个正整数:x,y和v。表示景点x到景点y之间有一条双向公路,车辆必须以速度v在该公路上行驶。 最后一行包含两个正整数s,t,表示想知道从景点s到景点t最大最小速度比最小的路径。s和t不可能相同。

Output

如果景点s到景点t没有路径,输出“IMPOSSIBLE”。否则输出一个数,表示最小的速度比。如果需要,输出一个既约分数。

Sample Input

【样例输入1】
4 2
1 2 1
3 4 2
1 4
【样例输入2】
3 3
1 2 10
1 2 5
2 3 8
1 3
【样例输入3】
3 2
1 2 2
2 3 4
1 3

Sample Output

【样例输出1】
IMPOSSIBLE
【样例输出2】
5/4
【样例输出3】
2

HINT

 

【数据范围】

1<  N < = 500

1 < = x, y < = N,0 < v < 30000,x ≠ y

0 < M < =5000

 

Source

 

 
posted @ 2014-11-14 18:29  iwtwiioi  阅读(553)  评论(0编辑  收藏  举报