BZOJ 1050 HAOI 2006 旅行comf SPFA动态加点

题目大意:给出S和T,求从S到T的最长边/最短边的最小值(分数形式输出)。


思路:和NOI2014的魔法森林很像啊,比较裸地动态加边,按照边的权值从大到小排序,然后一条一条的加进去,f[i]维护的是从S到i的路径上的最长边权的最小值,这样任意一个时刻,f[i]是S到i的最长边的最小值,当前加进去的边是所有边的最小值,计算答案更新答案就可以了。注意输出的判断。


CODE:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 10010
#define INF 1e6
using namespace std;

queue<int> q;

struct Complex{
	int x,y,len;

	bool operator <(const Complex &a)const {
		return len > a.len;
	}
	void Read() {
		scanf("%d%d%d",&x,&y,&len);
	}
}edge[MAX];

int points,edges;
int head[MAX],total;
int next[MAX],aim[MAX],length[MAX];
int s,t;

int f[MAX];
bool v[MAX];

inline void Add(int x,int y,int len)
{
	next[++total] = head[x];
	aim[total] = y;
	length[total] = len;
	head[x] = total;
}

inline void SPFA()
{
	while(!q.empty()) {
		int x = q.front(); q.pop();
		v[x] = false;
		for(int i = head[x]; i; i = next[i])
			if(f[aim[i]] > max(f[x],length[i])) {
				f[aim[i]] = max(f[x],length[i]);
				if(!v[aim[i]]) {
					v[aim[i]] = true;
					q.push(aim[i]);
				}
			}
	}
}

int main()
{
	cin >> points >> edges;
	for(int x,y,z,i = 1; i <= edges; ++i)
		edge[i].Read();
	sort(edge + 1,edge + edges + 1);
	cin >> s >> t;
	memset(f,0x3f,sizeof(f));
	f[s] = 0;
	double ans = INF;
	int up,down;
	for(int i = 1; i <= edges; ++i) {
		Add(edge[i].x,edge[i].y,edge[i].len);
		Add(edge[i].y,edge[i].x,edge[i].len);
		q.push(edge[i].x),q.push(edge[i].y);
		SPFA();
		double temp = static_cast<double>(f[t]) / edge[i].len;
		if(ans > temp) {
			ans = temp;
			up = f[t];
			down = edge[i].len;
		}
	}
	if(ans >= INF)	puts("IMPOSSIBLE");
	else {
		int gcd = __gcd(up,down);
		up /= gcd;
		down /= gcd;
		if(down != 1)	cout << up << '/' << down << endl;
		else	cout << up << endl;
	}
	return 0;
}


你可能感兴趣的:(SPFA,bzoj,动态加点,HAOI2006)