首页 > 其他 > 详细

网络流24题

时间:2019-09-25 18:04:19      阅读:119      评论:0      收藏:0      [点我收藏+]

马上就国庆了,翻了就是初赛。偶有怠惰,是复习联赛略枯燥的故。于是挖新坑,刷刷有趣的算法自慰。(肯定不会咕咕咕的啦,吧)

飞行员配对方案问题

输出方案数

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long
 
#define ON_DEBUGG
 
#ifdef ON_DEBUGG
 
#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC)

#else
 
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
 
#endif
 
using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;
 
template<typename ATP>inline ATP Max(ATP a, ATP b){
    return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
    return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
    return a < 0 ? -a : a;
}

const int N = 1007;
const int M = 2007;

struct Edge{
    int nxt, pre;
}e[M];
int head[N], cntEdge = 1;
inline void add(int u, int v){
    e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}

int n1, n2;
int vis[N], tim, match[N];
inline bool Hungry(int u){
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(vis[v] == tim) continue;
        vis[v] = tim;
        if(!match[v] || Hungry(match[v])){
            match[v] = u;
            return true;
        }
    }
    return false;
}

struct Answer{
    int x, y;
    bool operator < (const Answer &com) const{
        return x < com.x;
    }
}ans[N << 1];
int main(){
    io >> n1 >> n2;
    int u, v;
    while(~scanf("%d%d", &u, &v) && u != -1){
        add(u, v + n1);
    }
    
    int sum = 0;
    R(i,1,n1){
        ++tim;
        sum += Hungry(i);
    }
    
    if(sum == 0){
        printf("No Solution!");
        return 0;
    }
    printf("%d\n", sum);
    
    int tot = 0;
    R(i,1,n2){
        if(match[i + n1]){
//          printf("%d %d\n", match[i + n1], i);
            ans[++tot] = (Answer){ match[i + n1], i};
        }
    }
    sort(ans + 1, ans + tot + 1);
    R(i,1,tot){
        printf("%d %d\n", ans[i].x, ans[i].y);
    }
    return 0;
}

技术分享图片

网络流24题

原文:https://www.cnblogs.com/bingoyes/p/11585407.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!