Selecting Courses POJ - 2239(我是沙雕吧 按时间点建边 || 匹配水题)

呃呃呃呃呃

把每个课给了INF个容量。。。。我是沙雕把。。。。emm。。。。这题就是做着玩。。。呃呃呃别当真。。。。

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + 10, INF = 0x7fffffff;

int head[maxn], cur[maxn], d[maxn], nex[maxn << 1];
int n, m, s, t;
int cnt;

struct node
{
    int u, v, c;
}Node[maxn << 1];

void add_(int u, int v, int c)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt].c = c;
    nex[cnt] = head[u];
    head[u] = cnt++;
}

void add(int u, int v, int c)
{
    add_(u, v, c);
    add_(v, u, 0);
}

bool bfs()
{
    queue<int> Q;
    mem(d, 0);
    Q.push(s);
    d[s] = 1;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        for(int i = head[u]; i != -1; i = nex[i])
        {
            int v = Node[i].v;
            if(!d[v] && Node[i].c > 0)
            {
                d[v] = d[u] + 1;
                Q.push(v);
                if(v == t) return 1;
            }
        }
    }
    return d[t] != 0;
}

int dfs(int u, int cap)
{
    int ret = 0;
    if(u == t || cap == 0) return cap;
    for(int &i = cur[u]; i != -1; i = nex[i])
    {
        int v = Node[i].v;
        if(d[v] == d[u] + 1 && Node[i].c > 0)
        {
            int V = dfs(v, min(Node[i].c, cap));
            Node[i].c -= V;
            Node[i ^ 1].c += V;
            ret += V;
            cap -= V;
            if(cap == 0) break;
        }
    }
    if(cap > 0) d[u] = -1;
    return ret;
}

int Dinic()
{
    int ans = 0;
    while(bfs())
    {
        memcpy(cur, head, sizeof head);
        ans += dfs(s, INF);
    }
   return ans;
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        mem(head, -1);
        cnt = 0;
        int u, v;
        s = 0, t = 1000;
        rap(i, 1, n)
        {
            rd(m);
            add(s, 85 + i, 1);
            rap(j, 1, m)
            {
                rd(u), rd(v);
                add(85 + i, (u - 1) * 12 + v, 1);
            }
        }
        rap(i, 1, 85)
            add(i, t, 1);
        cout << Dinic() << endl;
    }

    return 0;
}

原文地址:https://www.cnblogs.com/WTSRUVF/p/10028578.html

时间: 2024-08-26 12:44:31

Selecting Courses POJ - 2239(我是沙雕吧 按时间点建边 || 匹配水题)的相关文章

POJ 1936 All in All 匹配, 水题 难度:0

题目 http://poj.org/problem?id=1936 题意 多组数据,每组数据有两个字符串A,B,求A是否是B的子串.(注意是子串,也就是不必在B中连续) 思路 设置计数器cnt为当前已匹配A的长度,明显在扫描B的过程中只需要记住cnt这一个状态. 扫描B,每次与A[cnt]匹配就将计数器增加1,cnt与A的长度一致时A就是B的子串. 感想 这道题也许可以用更复杂的方法. 代码 1 #include <cstdio> 2 #include <cstring> 3 #i

POJ 3312 Mahershalalhashbaz, Nebuchadnezzar, and Billy Bob Benjamin Go to the Regionals (水题,贪心)

题意:给定 n 个字符串,一个k,让你把它们分成每组k个,要保证每组中每个字符串长度与它们之和相差不能超2. 析:贪心策略就是长度相差最小的放上块. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <functional> #include <cstdlib> #includ

POJ 2239 Selecting Courses(二分匹配)

题目链接:http://poj.org/problem?id=2239 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming is a student who loves study every much, and at the beginning

POJ 2239 Selecting Courses【最大匹配】

大意: 有一些课程,每个课程的上课时间在每周可能会有多节,你可以从中任选一个时间去听课, 每周七天一天12节课  告诉你每节课的上课时间问在不冲突的情况下最多上几节课? 分析: 左集合课程 右集合时间 边为该节课对应的上课时间 求最大匹配就行了 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std

poj——2239 Selecting Courses

poj——2239   Selecting Courses Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10656   Accepted: 4814 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the c

Poj 2239 Selecting Courses 【二分匹配】

Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9007 Accepted: 4010 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming i

POJ 2239 二分图匹配/Hungary

Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8667 Accepted: 3863 Description It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming i

poj2239 Selecting Courses --- 二分图最大匹配

匈牙利算法模板题 有n门课程,每门课程可能有不同一时候间,不同一时候间的课程等价. 问不冲突的情况下最多能选多少门课. 建立二分图,一边顶点表示不同课程,还有一边表示课程的时间(hash一下). #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include

Hdoj 3697 Selecting courses 【贪心】

Selecting courses Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 2082    Accepted Submission(s): 543 Problem Description A new Semester is coming and students are troubling for selecting cours