题意:给一图,n个点,m条边,每条边有个花费,给出q条可疑的边,每条边有新的花费,每条可疑的边出现的概率相同,求不能经过原来可疑边
(可以经过可疑边新的花费构建的边),注意每次只出现一条可疑的边,n个点相互连通的最小花费的期望。
析:要想连通先让他们连通起来,先构造出一个MST,然后再暴力,如果这个边不在这里面,那么花费不变,如果在里面,那我们需要知道是用原来的边最少,
还是再找一条边使他们连通起来,这里就要先预处理了,dp[i]j[i] 表示 左边的那个一半 i 和 右边那一半 j 的最长距离,如果我们知道了,就可以用这个来比较了,
我们要对MST进行 n 次更新,每次遍历是 n,所以时间复杂度是 O(n*n),可以实现。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> //#include <tr1/unordered_map> #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; //using namespace std :: tr1; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const LL LNF = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 3e3 + 5; const LL mod = 10000000000007; const int N = 1e6 + 5; const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); } int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline int Min(int a, int b){ return a < b ? a : b; } inline int Max(int a, int b){ return a > b ? a : b; } inline LL Min(LL a, LL b){ return a < b ? a : b; } inline LL Max(LL a, LL b){ return a > b ? a : b; } inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Node{ int u, v, val; Node() { } Node(int uu, int vv, int va) : u(uu), v(vv), val(va) { } bool operator < (const Node &p) const { return val < p.val; } }; struct Edge{ int to, next; }; Edge edge[maxn<<1]; Node a[maxn*maxn]; int p[maxn], dist[maxn][maxn], head[maxn]; int dp[maxn][maxn]; bool is_tree[maxn][maxn]; int cnt, sum; int Find(int x) { return x == p[x] ? x : p[x] = Find(p[x]); } void add(int u, int v){ edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++; } void Kruskal(){ sort(a, a+m); int cnt = 0; sum = 0; for(int i = 0; i < m; ++i){ int x = Find(a[i].u); int y = Find(a[i].v); if(x != y){ p[y] = x; add(a[i].u, a[i].v); add(a[i].v, a[i].u); is_tree[a[i].u][a[i].v] = is_tree[a[i].v][a[i].u] = true; sum += a[i].val; ++cnt; } if(cnt == n-1) break; } } int dfs(int u, int fa, int root){ int ans = fa == root ? INF : dist[root][u]; //i d scf h for(int i = head[u]; ~i; i = edge[i].next){ int v = edge[i].to; if(v == fa) continue; int tmp = dfs(v, u, root); ans = Min(ans, tmp); dp[u][v] = dp[v][u] = Min(dp[u][v], tmp); } return ans; } int main(){ while(scanf("%d %d", &n, &m) == 2 && m+n){ for(int i = 0; i < n; ++i) p[i] = i; int u, v, c; memset(dist, INF, sizeof dist); for(int i = 0; i < m; ++i){ scanf("%d %d %d", &u, &v, &c); a[i] = Node(u, v, c); dist[u][v] = dist[v][u] = c; } memset(head, -1, sizeof head); memset(is_tree, false, sizeof is_tree); cnt = 0; Kruskal(); memset(dp, INF, sizeof dp); for(int i = 0; i < n; ++i) dfs(i, -1, i); scanf("%d", &m); double ans = 0.0; for(int i = 0; i < m; ++i){ scanf("%d %d %d", &u, &v, &c); if(!is_tree[u][v]) ans += sum; else ans += sum - dist[u][v] + Min(c, dp[u][v]); } printf("%.4f ", ans/m); } return 0; }