【】 To 09/18/2019

to

Kruskal

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5,M=5e5+5,inf=0x3f3f3f3f;
int n,m;
ll ans=0;
template<class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}
struct edge{
    int u,v,w;
    bool operator<(const edge&X)const{return w<X.w;}
}e[M];
int f[N];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
void kruskal(){
    for(int i=1;i<=n;++i) f[i]=i;
    for(int i=1,u,v;i<=m;++i)
        if(find(u=e[i].u)!=find(v=e[i].v)) f[f[u]]=f[v],ans+=e[i].w;
}

int main(){
    rd(n),rd(m);
    for(int i=1,u,v,w;i<=m;++i) rd(u),rd(v),rd(w),e[i]=(edge){u,v,w};
    sort(e+1,e+m+1);
    kruskal();
    printf("%lld",ans);
    return 0;
}

prim

#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int>pii;
priority_queue<pii,vector<pii>,greater<pii> >q;
const int N=2e5+5,M=5e5+5,inf=0x3f3f3f3f;
int n,m;
ll ans=0;
template<class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

int head[N],tot=0;
struct edge{int v,w,nxt;}e[M<<1];
void add(int u,int v,int w){
    e[++tot]=(edge){v,w,head[u]},head[u]=tot;
}

int dis[N],vis[N];
void prim(){
    memset(vis,0,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    q.push(make_pair(dis[1]=0,1));
    while(!q.empty()){
        int u=q.top().second;q.pop();
        if(vis[u]) continue;
        ans+=dis[u],vis[u]=1;
        for(int i=head[u],v,w;i;i=e[i].nxt)
        if(!vis[v=e[i].v]&&dis[v]>(w=e[i].w))
        dis[v]=w,q.push(make_pair(dis[v],v));
    }
}

int main(){
    rd(n),rd(m);
    for(int i=1,u,v,w;i<=m;++i) rd(u),rd(v),rd(w),add(u,v,w),add(v,u,w);
    prim();
    printf("%lld",ans);
    return 0;
}

Corridor Songkran

Some increase in the given tree sides so that it becomes the tree is not complete graph and the minimum spanning tree of FIG.

Since the tree to ensure that it has not even the minimum spanning tree of FIG edges have their smallest edge weight +1

Collection \ (S_x \) and \ (S_y \) will be a total increase between \ (| S_x | * | S_Y | -1 \) edges

#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int>pii;
priority_queue<pii,vector<pii>,greater<pii> >q;
const int N=6000+5,M=5e5+5,inf=0x3f3f3f3f;
int n,m,f[N],sz[N];
ll ans;
template<class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

struct edge{
    int u,v,w;
    bool operator<(const edge&X)const{return w<X.w;}
}e[N];
int find(int x){return x==f[x]?f[x]:f[x]=find(f[x]);}
void kruskal(){
    for(int i=1;i<=n;++i) f[i]=i,sz[i]=1;
    for(int i=1,x,y;i<n;++i){
        if((x=find(e[i].u))==(y=find(e[i].v))) continue;
        ans+=(ll)(e[i].w+1)*(sz[x]*sz[y]-1);
        f[x]=y,sz[y]+=sz[x];
    }
}

int main(){
    int T;rd(T);
    while(T--){
        rd(n);ans=0;
        for(int i=1,u,v,w;i<n;++i) rd(u),rd(v),rd(w),e[i]=(edge){u,v,w};
        sort(e+1,e+n);
        kruskal();
        printf("%lld\n",ans);
    } 
    return 0;
}

POJ1639 Picnic Planning

Given a \ (N \) points \ (M \) no edges of a minimum spanning tree is obtained which satisfies the node # 1 in FIG reading does not exceed a predetermined integer \ (S \)

CF891C Envy

CF891C luogu

== is to look at the yyb

  1. For any edge weights, the number of edges of all weights is constant minimum spanning tree
  2. For any properly bordered embodiment, after all of the connectivity graph edges completion of the addition of less than a certain weight is the same

Ownership considered together by two sides of the same value can be obtained even out of the result thereof is fixed

Connected to the side face of the communication block

Each pretreatment less than \ (W_i \) is added to join the edge of the right side \ (W_i \) side if the formation of a ring group then this query fails

In regards to modify the restore == Every time you want to look for the inquiry posed

#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int>pii;
const int N=5e5+5,M=5e5+5,inf=0x3f3f3f3f;
int n,m,s,tt,f[N];
template<class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

struct edge{
    int u,v,w,id;
    bool operator<(const edge&X)const{return w<X.w;}
}e[N],q[N];
bool cmp(edge X,edge Y){return X.id<Y.id;}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}

bool kruskal(){
    int K;rd(K);
    for(int i=1,x;i<=K;++i) rd(x),q[i]=e[x];
    sort(q+1,q+K+1);
    for(int i=1,j=1;i<=K;i=++j){
        while(j<K&&q[j+1].w==q[j].w) ++j;
        for(int k=i;k<=j;++k) f[q[k].u]=q[k].u,f[q[k].v]=q[k].v;
        for(int k=i;k<=j;++k){
            if(find(q[k].u)==find(q[k].v)) return 0;
            f[f[q[k].u]]=f[q[k].v];
        }
    }
    return 1;
}


int main(){
    freopen("in.txt","r",stdin);
    rd(n),rd(m);
    for(int i=1,u,v,w;i<=m;++i) rd(u),rd(v),rd(w),e[i]=(edge){u,v,w,i};
    for(int i=1;i<=n;++i) f[i]=i;
    sort(e+1,e+m+1);
    for(int i=1,j=1;i<=m;i=++j){
        while(j<m&&e[j+1].w==e[j].w) ++j;
        for(int k=i;k<=j;++k) e[k].u=find(e[k].u),e[k].v=find(e[k].v);
        for(int k=i;k<=j;++k)
        if(find(e[k].u)!=find(e[k].v)) f[f[e[k].u]]=f[e[k].v];
    }
    sort(e+1,e+m+1,cmp);
    rd(m);while(m--) puts(kruskal()?"YES":"NO");
    return 0;
}

[JLOI2011] flight path

Shortest layered templates? Pay attention to use dijkstra

There is a set of data == m <k so to output \ (min (dis [t] [i], ans) \)

Four times the experience ==

#include<bits/stdc++.h>
using namespace std;
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define ll long long
const int N=10000+10,M=50000+10,inf=0x3f3f3f3f;
int n,m,K,s,t;
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

int head[N],tot=0;
struct edge{int v,w,nxt;}e[M<<1];
void add(int u,int v,int w){
    e[++tot]=(edge){v,w,head[u]},head[u]=tot;
}

int dis[N][12];bool vis[N][12];
struct node{
    int dis,id,us;
    bool operator>(const node&X)const{return dis>X.dis;}
};
priority_queue<node,vector<node>,greater<node> >q;
void dij(){
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    q.push((node){0,s,0}),dis[s][0]=0;
    while(!q.empty()){
        node nw=q.top();q.pop();
        int u=nw.id,us=nw.us;
        if(vis[u][us]) continue;
        vis[u][us]=1;
        for(int i=head[u],v,w;i;i=e[i].nxt){
            if(dis[v=e[i].v][us]>dis[u][us]+(w=e[i].w))//不用 
                q.push((node){dis[v][us]=dis[u][us]+(w=e[i].w),v,us});
            if(us<K&&dis[v][us+1]>dis[u][us])
                if(!vis[v][us+1]) q.push((node){dis[v][us+1]=dis[u][us],v,us+1});
        }
    }
}

int main(){
    freopen("in.txt","r",stdin);
    rd(n),rd(m),rd(K),rd(s),rd(t),++s,++t;
    for(int i=1,u,v,w;i<=m;++i) rd(u),rd(v),rd(w),add(++u,++v,w),add(v,u,w);
    spfa();
    if(dis[t][K]==inf) return puts("-1"),0;
    int ans=inf;
    for(int i=0;i<=K;++i) ans=Min(ans,dis[t][i]);
    printf("%d",ans);
    return 0;
}

[USACO14OPEN] GPS duel Dueling GPS's

luoguP3106 bzoj3538

hin water? They were run three times dijkstra

#include<bits/stdc++.h>
using namespace std;
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define ll long long
const int N=10000+10,M=50000+10,inf=0x3f3f3f3f;
typedef pair<int,int>pii;
int n,m,K,s,t,w2[M];
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}


int head[N],tot=0;
struct edge{int v,w,nxt;}e[M];
void add(int u,int v,int w){
    e[++tot]=(edge){v,w,head[u]},head[u]=tot;
}

int dis[N],nw[M],tg[M];bool vis[N];
priority_queue<pii,vector<pii>,greater<pii> >q;
void dij(){
    memset(nw,0,sizeof(nw));
    memset(vis,0,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    dis[s]=0,q.push(make_pair(0,s));
    while(!q.empty()){
        int u=q.top().second;q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(int i=head[u],v,w;i;i=e[i].nxt)
        if(dis[v=e[i].v]>dis[u]+(w=e[i].w)){
            ++tg[nw[v]],nw[v]=i,--tg[i];
            q.push(make_pair(dis[v]=dis[u]+w,v));
        }
    }
} 

int main(){
    freopen("in.txt","r",stdin);
    rd(n),rd(m),s=n;
    for(int i=1,u,v,w1;i<=m;++i) rd(u),rd(v),rd(w1),rd(w2[i]),add(v,u,w1),tg[i]=2;
    dij();
    for(int i=1;i<=m;++i) e[i].w=w2[i];
    dij();
    for(int i=1;i<=m;++i) e[i].w=tg[i];
    dij();
    printf("%d",dis[1]);
    return 0;
}

Nuclear tree network

  • Enumeration \ (O (n ^ 3) \)

    Bfs seeking a diameter twice as a distance not exceeding the enumeration \ (S \) two points \ (p, q \) and then each node on the core from starting to take from the minimum value seek

  • Greedy enumeration + \ (O (n ^ 2) \)

    You can know \ (p, q \) farther away as possible so only two points in diametrically enumeration \ (P \) point is then calculated directly from its \ (S \) a \ (Q \) point

  • Binary \ (O (n \ log \ SUM) \)

    The answer may be found to have a monotonic binary minimum eccentricity may check whether there is a core

  • Monotone queue \ (O (n) \)

Goo ==

luoguP2245 interplanetary navigation

Review it again trucking?

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<stack>
#include<algorithm>
using namespace std;
#define ll long long
#define rg register
const int N=100000+5,M=500000+5,inf=0x3f3f3f3f,P=19650827;
int n,m,q;
int dep[N],p[N][25],w[N][25];
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

struct edg{int u,v,w;}E[M];
bool cmp(edg a,edg b){return a.w<b.w;}

int head[N],tot=0,ans;
struct edge{int v,nxt,w;}e[M];
void add(int u,int v,int w){
    e[++tot]=(edge){v,head[u],w};head[u]=tot;
}
int f[N];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
void kruskal(){
    for(int i=1;i<=n;++i) f[i]=i;
    for(int i=1,u,v,cnt=0;i<=m;++i){
        if(find(u=E[i].u)!=find(v=E[i].v)){
            f[f[u]]=f[v],++cnt;
            add(u,v,E[i].w),add(v,u,E[i].w);
            if(cnt==n-1) break;
        }
    }
}

void dfs(int u){
    for(int i=head[u],v;i;i=e[i].nxt)
        if(!dep[v=e[i].v]) dep[v]=dep[u]+1,p[v][0]=u,w[v][0]=e[i].w,dfs(v);
}
void build(){
    for(int i=1;i<=n;++i)
        if(!dep[i]) dep[i]=1,p[i][0]=0,dfs(i);
    dfs(1);
    for(int i=1; i<=20; i++)
        for(int j=1; j<=n; j++)
            p[j][i]=p[p[j][i-1]][i-1],
            w[j][i]=max(w[j][i-1], w[p[j][i-1]][i-1]);
}

int LCA(int a,int b){
    ans=0;
    if(dep[a]>dep[b]) swap(a,b);
    for(int i=20;i>=0;--i){
        if(dep[p[b][i]]<dep[a]) continue;
        ans=max(ans,w[b][i]);b=p[b][i];
    }
    if(a==b) return ans;
    for(int i=20;i>=0;--i){
        if(p[a][i]==p[b][i]) continue;
        ans=max(ans,max(w[a][i],w[b][i]));
        a=p[a][i],b=p[b][i];
    }
    ans=max(ans,max(w[a][0],w[b][0]));
    return ans;
}

int main(){
    freopen("in.txt","r",stdin);
    rd(n),rd(m);
    for(int i=1,u,v,w;i<=m;++i) rd(u),rd(v),rd(w),E[i]=(edg){u,v,w};
    sort(E+1,E+m+1,cmp);
    kruskal(),build();
    rd(q);
    for(int i=1,u,v;i<=q;++i){
        rd(u),rd(v);
        if(find(u)!=find(v)) puts("impossible");
        else printf("%d\n",LCA(u,v));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/lxyyyy/p/11545891.html