• 网络流+最小生成树的最少割边数--How Many to Be Happy?


    题意:https://blog.csdn.net/Ratina/article/details/95200594

    思路:

    首先我们知道最小生成树就是按长度枚举边,能连就连。

    那么,如果这条边在最小生成树里,那我们只需要看比它短的边是不是已经使当前的u---v连通,如果连通最少需要切掉几条(边权为1跑最小割)。

    所以我们对边排序,枚举边+重构图跑Dinic就行了。

      1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
      2 #include <cstdio>//sprintf islower isupper
      3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
      4 #include <iostream>//pair
      5 #include <fstream>//freopen("C:\Users\13606\Desktop\Input.txt","r",stdin);
      6 #include <bitset>
      7 //#include <map>
      8 //#include<unordered_map>
      9 #include <vector>
     10 #include <stack>
     11 #include <set>
     12 #include <string.h>//strstr substr
     13 #include <string>
     14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
     15 #include <cmath>
     16 #include <deque>
     17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
     18 #include <vector>//emplace_back
     19 //#include <math.h>
     20 #include <cassert>
     21 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
     22 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
     23 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
     24 //******************
     25 int abss(int a);
     26 int lowbit(int n);
     27 int Del_bit_1(int n);
     28 int maxx(int a,int b);
     29 int minn(int a,int b);
     30 double fabss(double a);
     31 void swapp(int &a,int &b);
     32 clock_t __STRAT,__END;
     33 double __TOTALTIME;
     34 void _MS(){__STRAT=clock();}
     35 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
     36 //***********************
     37 #define rint register int
     38 #define fo(a,b,c) for(rint a=b;a<=c;++a)
     39 #define fr(a,b,c) for(rint a=b;a>=c;--a)
     40 #define mem(a,b) memset(a,b,sizeof(a))
     41 #define pr printf
     42 #define sc scanf
     43 #define ls rt<<1
     44 #define rs rt<<1|1
     45 typedef vector<int> VI;
     46 typedef long long ll;
     47 const double E=2.718281828;
     48 const double PI=acos(-1.0);
     49 //const ll INF=(1LL<<60);
     50 const int inf=(1<<30);
     51 const double ESP=1e-9;
     52 const int mod=(int)1e9+7;
     53 const int N=(int)1e3+10;
     54 const int M=(int)5e3+5;
     55 
     56 class DINIC
     57 {
     58 public:
     59 //    const int MAXN=10004,MAXWAY=100005;
     60     int n,way,max_flow,deep[N];
     61     int tot,head[N],cur[N];
     62     struct EDGE{
     63         int to,next;
     64         int dis;
     65     }edge[M];
     66     void Init(int n_)
     67     {
     68         tot=-1;//因为加反向边要^1,所以要从0开始;
     69         n=n_;
     70         max_flow=0;
     71         for(int i=0;i<=n_;++i)
     72             head[i]=-1;
     73     }
     74     void add(int from,int to,int V)
     75     {
     76         //正向
     77         ++tot;
     78         edge[tot].to=to;
     79         edge[tot].dis=V;
     80         edge[tot].next=head[from];
     81         head[from]=tot;
     82         //反向
     83         swap(from,to);
     84         ++tot;
     85         edge[tot].to=to;
     86         edge[tot].dis=V;
     87         edge[tot].next=head[from];
     88         head[from]=tot;
     89     }
     90     queue<int>q;
     91     bool bfs(int s,int t)
     92     {
     93         for(int i=1;i<=n;++i)
     94             deep[i]=inf;
     95         while(!q.empty())q.pop();
     96         for(int i=1;i<=n;++i)cur[i]=head[i];
     97         deep[s]=0;
     98         q.push(s);
     99 
    100         while(!q.empty())
    101         {
    102             int now=q.front();q.pop();
    103             for(int i=head[now];i!=-1;i=edge[i].next)
    104             {
    105                 if(deep[edge[i].to]==inf&&edge[i].dis)
    106                 {
    107                     deep[edge[i].to]=deep[now]+1;
    108                     q.push(edge[i].to);
    109                 }
    110             }
    111         }
    112         return deep[t]<inf;
    113     }
    114     int dfs(int now,int t,int limit)
    115     {
    116         if(!limit||now==t)return limit;
    117         int flow=0,f;
    118         for(int i=cur[now];i!=-1;i=edge[i].next)
    119         {
    120             cur[now]=i;
    121             if(deep[edge[i].to]==deep[now]+1&&(f=dfs(edge[i].to,t,min(limit,edge[i].dis))))
    122             {
    123                 flow+=f;
    124                 limit-=f;
    125                 edge[i].dis-=f;
    126                 edge[i^1].dis+=f;
    127                 if(!limit)break;
    128             }
    129         }
    130         return flow;
    131     }
    132     void Dinic(int s,int t)
    133     {
    134         while(bfs(s,t))
    135             max_flow+=dfs(s,t,inf);
    136     }
    137 }G;
    138 struct EDGE
    139 {
    140     int u,v;
    141     int val;
    142     friend bool operator<(EDGE a,EDGE b)
    143     {
    144         return a.val<b.val;
    145     }
    146 }edge[M];
    147 
    148 int main()
    149 {
    150     int n,m;
    151     sc("%d%d",&n,&m);
    152     for(int i=1;i<=m;++i)
    153         sc("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].val);
    154     sort(edge+1,edge+1+m);
    155     int ans=0;
    156     for(int i=1;i<=m;++i)
    157     {
    158         G.Init(n);
    159         for(int j=1;j<i;++j)
    160         {
    161             if(edge[j].val<edge[i].val)
    162                 G.add(edge[j].u,edge[j].v,1);
    163         }
    164         G.Dinic(edge[i].u,edge[i].v);
    165         ans+=G.max_flow;
    166     }
    167     pr("%d
    ",ans);
    168     return 0;
    169 }
    170 
    171 /**************************************************************************************/
    172 
    173 int maxx(int a,int b)
    174 {
    175     return a>b?a:b;
    176 }
    177 
    178 void swapp(int &a,int &b)
    179 {
    180     a^=b^=a^=b;
    181 }
    182 
    183 int lowbit(int n)
    184 {
    185     return n&(-n);
    186 }
    187 
    188 int Del_bit_1(int n)
    189 {
    190     return n&(n-1);
    191 }
    192 
    193 int abss(int a)
    194 {
    195     return a>0?a:-a;
    196 }
    197 
    198 double fabss(double a)
    199 {
    200     return a>0?a:-a;
    201 }
    202 
    203 int minn(int a,int b)
    204 {
    205     return a<b?a:b;
    206 }
  • 相关阅读:
    bzoj1007: [HNOI2008]水平可见直线(单调栈)
    1264: [AHOI2006]基因匹配Match(动态规划神题)
    bzoj1433: [ZJOI2009]假期的宿舍(最大二分图匹配)
    bzoj3931: [CQOI2015]网络吞吐量(spfa+网络流)
    [ZJOI2007]矩阵游戏
    [HAOI2007]覆盖问题
    [ZJOI2008]树的统计
    [ZJOI2010]数字计数
    [HAOI2006]旅行
    [HAOI2006]数字序列
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/11769748.html
Copyright © 2020-2023  润新知