zoukankan      html  css  js  c++  java
  • [swust1736]飞行员配对方案问题(最大流,记录路径)

    题目链接:https://www.oj.swust.edu.cn/problem/show/1736

    很裸的二分图,但是不知道咋回事。匈牙利跑不出解路径。没办法上了最大流。

    输出解很简单,因为容量是1,所以只能满流。跑完最大流后查看残余网络,找剩余容量为0的边,输出出点和入点值就行了。

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 
      4 typedef struct Edge {
      5     int u, v, w, next;
      6 }Edge;
      7 
      8 const int inf = 0x7f7f7f7f;
      9 const int maxn = 1111;
     10 
     11 int cnt, dhead[maxn];
     12 int cur[maxn], dd[maxn];
     13 Edge dedge[maxn<<1];
     14 // bool vis[maxn]; // 记录经过的点
     15 int S, T, N;
     16 
     17 void init() {
     18     memset(dhead, -1, sizeof(dhead));
     19     for(int i = 0; i < maxn; i++) dedge[i].next = -1;
     20     S = 0; cnt = 0;
     21 }
     22 
     23 void adde(int u, int v, int w, int c1=0) {
     24     dedge[cnt].u = u; dedge[cnt].v = v; dedge[cnt].w = w; 
     25     dedge[cnt].next = dhead[u]; dhead[u] = cnt++;
     26     dedge[cnt].u = v; dedge[cnt].v = u; dedge[cnt].w = c1; 
     27     dedge[cnt].next = dhead[v]; dhead[v] = cnt++;
     28 }
     29 
     30 bool bfs(int s, int t, int n) {
     31     // memset(vis, 0, sizeof(vis));
     32     queue<int> q;
     33     for(int i = 0; i < n; i++) dd[i] = inf;
     34     dd[s] = 0;
     35     q.push(s);
     36     while(!q.empty()) {
     37         int u = q.front(); q.pop();
     38         for(int i = dhead[u]; ~i; i = dedge[i].next) {
     39             if(dd[dedge[i].v] > dd[u] + 1 && dedge[i].w > 0) {
     40                 dd[dedge[i].v] = dd[u] + 1;
     41                 // vis[dedge[i].v] = 1;
     42                 if(dedge[i].v == t) return 1;
     43                 q.push(dedge[i].v);
     44             }
     45         }
     46     }
     47     return 0;
     48 }
     49 
     50 int dinic(int s, int t, int n) {
     51     int st[maxn], top;
     52     int u;
     53     int flow = 0;
     54     while(bfs(s, t, n)) {
     55         for(int i = 0; i < n; i++) cur[i] = dhead[i];
     56         u = s; top = 0;
     57         while(cur[s] != -1) {
     58             if(u == t) {
     59                 int tp = inf;
     60                 for(int i = top - 1; i >= 0; i--) {
     61                     tp = min(tp, dedge[st[i]].w);
     62                 }
     63                 flow += tp;
     64                 for(int i = top - 1; i >= 0; i--) {
     65                     dedge[st[i]].w -= tp;
     66                     dedge[st[i] ^ 1].w += tp;
     67                     if(dedge[st[i]].w == 0) top = i;
     68                 }
     69                 u = dedge[st[top]].u;
     70             }
     71             else if(cur[u] != -1 && dedge[cur[u]].w > 0 && dd[u] + 1 == dd[dedge[cur[u]].v]) {
     72                 st[top++] = cur[u];
     73                 u = dedge[cur[u]].v;
     74             }
     75             else {
     76                 while(u != s && cur[u] == -1) {
     77                     u = dedge[st[--top]].u;
     78                 }
     79                 cur[u] = dedge[cur[u]].next;
     80             }
     81         }
     82     }
     83     return flow;
     84 }
     85 
     86 int n, m;
     87 
     88 int main() {
     89     // freopen("in", "r", stdin);
     90     init();
     91     int u, v;
     92     scanf("%d%d",&n,&m);
     93     S = 0, T = m + 1, N = T + 1;
     94     for(int i = 1; i <= n; i++) adde(S, i, 1);
     95     for(int i = n+1; i <= m; i++) adde(i, T, 1);
     96     while(~scanf("%d%d",&u,&v)&&u+v!=-2) adde(u, v, 1);
     97     printf("%d
    ", dinic(S, T, N));
     98     for(int i = 1; i <= n; i++) {
     99         for(int j = dhead[i]; ~j; j=dedge[j].next) {
    100             if(dedge[j].w == 0 && dedge[j].v != 0) printf("%d %d
    ", dedge[j].u, dedge[j].v);
    101         }
    102     }
    103     return 0;
    104 }
  • 相关阅读:
    js sort方法根据数组中对象的某一个属性值进行排序
    ES6之Array.from()方法
    windows安装gnvm安装教程,node多版本解决方案
    Singleton模式的两种实现形式
    java 一组数随机组合求和等于目标数
    与公式相关的代码片段
    项目总结【Excel中附件导出和导入】
    Hook钩子C#实例
    网上以讹传讹的逻辑推理题:电影票问题
    原创:仿AspNetPager写的JS分页控件,适合ajax调用
  • 原文地址:https://www.cnblogs.com/kirai/p/6793187.html
Copyright © 2011-2022 走看看