码迷,mamicode.com
首页 > 编程语言 > 详细

定根最小树形图 朱刘算法 luogu P4716

时间:2019-10-05 20:03:30      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:http   def   pair   while   fir   space   return   org   end   

https://www.luogu.org/problem/P4716

#include<bits/stdc++.h>
#define ll long long
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define forn(i,x,g,e) for(int i=g[x];i;i=e[i].next)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define fi first
#define se second
#define mp make_pair
#define pii pair<ll,ll>
#define all(x) x.begin(),x.end()
#define show(x) cout<<#x<<"="<<x<<endl
#define showa(a,b) cout<<#a<<‘[‘<<b<<"]="<<a[b]<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show4(w,x,y,z) cout<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show5(v,w,x,y,z) cout<<#v<<"="<<v<<" "<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define showa2(x,a,b) cout<<#x<<": ";rep(i,a,b) cout<<x[i]<<‘ ‘;cout<<endl
using namespace std;//head
const int maxn=1e3+10,maxm=1e4+10;
const ll INF=0x3f3f3f3f,mod=1e9+7;
int casn,n,m,k;
struct node{int a,b,c;}e[maxm];
int in[maxn],pre[maxn],vis[maxn],id[maxn];
ll mag(){
  ll ans=0;int cnt=0,a,b,laz;
  while(1){
    rep(i,1,n) in[i]=INF,id[i]=vis[i]=0;
    rep(i,1,m) if(e[i].a^e[i].b&&e[i].c<in[e[i].b])
      pre[e[i].b]=e[i].a,in[e[i].b]=e[i].c;
    in[k]=0;
    rep(i,1,n){
      if(in[i]==INF) return -1;
      ans+=in[i];
      for(a=i;a^k&&vis[a]^i&&!id[a];a=pre[a])vis[a]=i;
      if(a^k&&!id[a]){
        id[a]=++cnt;
        for(b=pre[a];a^b;b=pre[b])id[b]=cnt;
      }
    }
    if(!cnt) return ans;
    rep(i,1,n) if(!id[i]) id[i]=++cnt;
    rep(i,1,m) {
      laz=in[e[i].b];
      if((e[i].a=id[e[i].a])^(e[i].b=id[e[i].b]))
        e[i].c-=laz;
    }
    n=cnt;k=id[k],cnt=0;
  }
}
int main() {IO;
  cin>>n>>m>>k;
  rep(i,1,m)cin>>e[i].a>>e[i].b>>e[i].c;
  cout<<mag()<<endl;
}

 

定根最小树形图 朱刘算法 luogu P4716

标签:http   def   pair   while   fir   space   return   org   end   

原文地址:https://www.cnblogs.com/nervendnig/p/11625479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!