UVA - 1504 Genghis Khan the Conqueror

news/2024/11/16 10:44:38/

如果修改边权的边如果不是组成最小生成树的边,那么肯定不会有影响。
那么对于其他的边来说,就把它删了然后找到最短的替代边,和原树+修改的边权中选小的那个即可。
其实就是次小生成树的变种,只是保存每一条边删除以后的最小值罢了。

#include<iostream>
#include<string>
#include<cstdio>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<fstream>using namespace std;
typedef long long ll;//ifstream cin;const int maxn = 3500,maxe = maxn * maxn,INF = 0x3f3f3f3f;int n,m;struct node{int u,v,w;bool operator < (const node &a) const {return w < a.w;}
}e[maxe];bool isedge[maxn][maxn];vector<int> r[maxn];//生成树上的边,用于dfs
vector<node> mst;//mst上的每一条边
vector<int> pl;//删边以后一棵子树上的所有点int top[maxn];
int wei[maxn][maxn];//x-y的边权
int add[maxn][maxn];//删除x-y边之后加入边的边权bool vis[maxn];int find(int x){return x == top[x] ? x : top[x] = find(top[x]);
}void dfs(int fa,int u,int ed){//寻找删边以后u所在子树上的所有点if (u == ed) return;pl.push_back(u);vis[u] = true;for(int i = 0;i < r[u].size();++i){int p = r[u][i];if (p != fa && !vis[p]) dfs(u,p,ed);}
}int kruskal(){int ans = 0;mst.clear();for(int i = 0;i < n;++i) top[i] = i,r[i].clear();int sum = 1;for(int k = 0;k < m;++k){int u = e[k].u,v = e[k].v,w = e[k].w;int fu = find(u),fv = find(v);if (fu != fv){ans += w;top[fu] = fv;isedge[u][v] = isedge[v][u] = 1;r[u].push_back(v);r[v].push_back(u);mst.push_back(e[k]);sum++;}if (sum == n){break;}}memset(add,INF,sizeof add);for(int k = 0;k < mst.size();++k){//遍历每一条边,找到删除之后加入新边的最小值int u = mst[k].u,v = mst[k].v;pl.clear();memset(vis,0,sizeof vis);dfs(u,u,v);for(int t = 0;t < m;++t){//寻找新边int x = e[t].u,y = e[t].v,w = e[t].w;if (isedge[x][y]) continue;if ((vis[x] && vis[y]) || (!vis[x] && !vis[y])) continue;add[u][v] = add[v][u] = w;break;}}return ans;
}void init(){int a,b,c;memset(isedge,0,sizeof isedge);memset(e,0,sizeof e);memset(wei,0,sizeof wei);for(int i = 0;i < m;++i){cin >> a >> b >> c;e[i] = {a,b,c};wei[a][b] = wei[b][a] = c;}sort(e,e + m);int t,ans = 0;int kru = kruskal();cin >> t;for(int k = 0;k < t;++k){cin >> a >> b >> c;if (isedge[a][b]){ans += min(kru - wei[a][b] + c,kru - wei[a][b] + add[a][b]);}else ans += kru;}printf("%.4f\n",(double)ans * 1.0 / t);
}int main(){//cin.open("in.txt");while(cin >> n >> m && n && m){init();}
}

http://www.ppmy.cn/news/966743.html

相关文章

Khan Academy - Statistics and Probability - Unit 8 COUNTING, PERMUTATIONS, AND COMBINATIONS

COUNTING, PERMUTATIONS, AND COMBINATIONS PART 1 Counting principle and factorial (计数原理与阶乘) Part 2 Permutations (排列) Part 3 Combinations (组合) Part 4 Combinatorics and probability

Genghis Khan the Conqueror HDU - 4126(MST)

Genghis Khan(成吉思汗)(1162-1227), also known by his birth name Temujin(铁木真) and temple name Taizu(元太祖), was the founder of the Mongol Empire and the greatest conqueror in Chinese history. After uniting many of the nomadic tribes on the Mongolian ste…

Khan Academy - Statistics and Probability - Unit 4 MODELING DATA DISTRIBUTIONS

MODELING DATA DISTRIBUTIONS PART 1 Percentiles PART 2 Z-scores PART 3 Effects of linear transformation PART 4 Density curves PART 5 Normal distribution and the empirical rule

Salman Khan加入 NFT 潮流,即将推出数字收藏品

印度新德里&#xff1a;在宝莱坞巨星 Amitabh Bachchan 加入元宇宙之后&#xff0c;另一位宝莱坞演员 Salman Khan 周三宣布&#xff0c;他将很快与 BollyCoin 合作推出NFT。 元宇宙是指物理、增强和虚拟现实在共享在线空间中的融合。 为了推出NFT&#xff0c;BollyCoin.com …

我摊牌了我不装了_不装了,我是FPX.Khan,我摊牌了!

1.Khan现身Doinb直播间&#xff0c;不装了&#xff0c;我是FPX.Khan&#xff0c;我摊牌了 Khan加盟FPX的消息早已被实锤&#xff0c;但FPX迟迟没有进行官宣。 不过在今日凌晨Doinb直播过程中&#xff0c;失踪人口Khan出现在了他的直播间&#xff0c;并让Doinb帮他点外卖。给观众…

刷题总结——Genghis Khan the Conqueror (hdu4126)

题目&#xff1a; Genghis Khan(成吉思汗)(1162-1227), also known by his birth name Temujin(铁木真) and temple name Taizu(元太祖), was the founder of the Mongol Empire and the greatest conqueror in Chinese history. After uniting many of the nomadic tribes on …

Khan Academy - Statistics and Probability - Unit 7 PROBABILITY

PROBABILITY PART 1 Basic theoretical probability PART 2 Basic set operations PART 3 Experimental probability PART 4 Multiplication rule for independent events PART 5 Multiplication ru

khan - linear algebra - Orthogonal complements

文章目录 Orthogonal complements V ⊥ V^\perp V⊥ is a subspace. N ( A ) ( R ( A ) ) ⊥ N(A)(R(A))^\perp N(A)(R(A))⊥ N ( B T ) ( C ( B ) ) ⊥ N(B^T)(C(B))^\perp N(BT)(C(B))⊥ d i m ( V ) d i m ( V ⊥ ) n dim(V)dim(V^\perp)n dim(V)dim(V⊥)nRepresenting …