当前位置: 首页 > 工具软件 > portkey > 使用案例 >

URAL 1447. Portkey Network

阳建弼
2023-12-01
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <ctime>
#include <cmath>
#include <bitset>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
const int maxn = 1002;
const int SIZE = 511111;
const double eps = 1e-10;
double dis[maxn][maxn];
double C[maxn][maxn], D[maxn][maxn];
double t[maxn];
bool vis[maxn];
double tc[maxn], td[maxn];
double prim(int n) {
	double x = 0, y = 0;
	int i, j, k;
	for (i = 1; i <= n; ++i)
		t[i] = dis[1][i], vis[i] = false, tc[i] = C[1][i], td[i] = D[1][i];
	vis[1] = true;
	for (i = 1; i < n; ++i) {
		double mi = 1e6;
		k = 1;
		for (j = 1; j <= n; ++j)
			if (!vis[j] && mi > t[j])
				mi = t[j], k = j;
		vis[k] = true;
		x += tc[k];
		y += td[k];
		for (j = 1; j <= n; ++j)
			if (!vis[j] && dis[k][j] < t[j])
				t[j] = dis[k][j], td[j] = D[k][j], tc[j] = C[k][j];
	}
	return y / x;
}
int u[SIZE], v[SIZE], c[SIZE], d[SIZE];
int cnt;
int main() {
	ios::sync_with_stdio(false);
	int i, j, n, m, a, b;
	scanf("%d%d", &n, &m);
	for (i = 0; i <= n; ++i)
		for (j = 0; j <= n; ++j)
			dis[i][j] = 1e70;
	double rate = 1e20, pre;
	for (i = 0; i < m; ++i) {
		scanf("%d%d%d%d", u + i, v + i, c + i, d + i);
		C[u[i]][v[i]] = C[v[i]][u[i]] = c[i];
		D[u[i]][v[i]] = D[v[i]][u[i]] = d[i];
	}
	rate = 1.0;
	cnt = 0;
	while (true) {
		pre = rate;
		++cnt;
		for (i = 0; i < m; ++i)
			dis[u[i]][v[i]] = dis[v[i]][u[i]] = d[i] - rate * c[i];
		rate = prim(n);
		if (fabs(rate - pre) < eps)
			break;
	}
	printf("%.10f\n", rate);
	return 0;
}

 类似资料:

相关阅读

相关文章

相关问答