0
点赞
收藏
分享

微信扫一扫

危险路径(启发式合并+mst)

双井暮色 2022-10-24 阅读 85


给定一个 n 个点 m 条边的连通无向图,其中点从 1 到 n 标号,而每条边有一个危险值。
对于任意一条路径,定义路径上危险值的最大值为这条路径的危险值。

对于任意不同的两点 u 和 v,定义 d(u, v) 为所有从 u 到 v 的路径的危险值最小值。
fu=∑u!=vd(u,v)
求⊕i=1n(i⋅f(i))

最小生成树+启发合并
对集合维护集合里的值和真值的差。

#include<bits/stdc++.h> 
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
#define
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int T,n,m;
#define
class bingchaji
{
public:
int father[MAXN],n,cnt,sz[MAXN];
vector<pair<ull,ull> > g[MAXN];
ull sum[MAXN];
void mem(int _n)
{
n=cnt=_n;
For(i,n) {
father[i]=i,sz[i]=1;
sum[i]=0; g[i].resize(0);
g[i].pb(mp(i,0));
}
}
int getfather(int x)
{
if (father[x]==x) return x;

return father[x]=getfather(father[x]);
}
void unite(int x,int y)
{
x=getfather(x);
y=getfather(y);
if (x^y) {
--cnt;
father[x]=y;
sz[y]+=sz[x];
}
}
bool same(int x,int y)
{
return getfather(x)==getfather(y);
}
}S;
#define
struct edge{
int u,v;
ull w;
friend bool operator<(edge a,edge b){
return a.w<b.w;
}
}e[MAXM];

int main()
{
// freopen("g.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
For(kcase,T) {
n=read(),m=read();
For(i,m) {
e[i].u=read();
e[i].v=read();
e[i].w=read();
}
sort(e+1,e+1+m);
S.mem(n);
For(i,m) {
{
int t1=e[i].u,t2=e[i].v;
ull v=e[i].w;
t1=S.getfather(t1);
t2=S.getfather(t2);
if(t1==t2) continue;

if (S.sz[t1]>S.sz[t2]) swap(t1,t2);
S.sum[t1]+=S.sz[t2]*v;
S.sum[t2]+=S.sz[t1]*v;
S.sz[t2]+=S.sz[t1];
for(pair<ull,ull> p:S.g[t1] ) {
S.g[t2].pb(mp(p.fi,p.se+S.sum[t1]-S.sum[t2]));
}
S.father[t1]=t2;

}
}
ull ret=0;
int u=S.getfather(1);
ull t=S.sum[u];
for(auto p:S.g[u]) {
ret^=1ULL * p.fi*(p.se+t);
}
printf("Case #%d: ",kcase);
cout<<ret<<endl;
}
return 0;
}


举报

相关推荐

0 条评论