kebab HDU - 2883(按时间段建点)

题意:

  有n个人去撸串,每个人都能决定自己的串上有几块肉,每一块肉都要花费一个单位时间才熟,烤炉一次能烤m块肉 

  给出每个人的起始时间、终止时间、要几串、每个串上有几块肉,问能否满足所有的人

(啥?题不是这么说的?。。。这样看就对了。。。)

解析:

  和hdu3572一样 只不过这个时间的范围比较大 所以就不能以时间点建点了  所以要以时间段建点

  把所有的时间放到数组里,这里我用的是vector,然后排序,去重,对于每个人 遍历所有的时间,如果有个时间段在当前人购买的时间里  就把人和这个时间段连边

  权值为 INF  把每个时间段和t连边 权值为(v[j + 1]  - v[j]) * m

  s 和 人连边 权值为p[i] * t[i]

  

#include 
#include 
#include 
#include 
#include 
#include 
#include <set>
#include 
#include 
#include 
#include 
#include 
#include 
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; 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 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
#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 = 1e4 + 10, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m, s, t, cnt;
int s_[maxn], e_[maxn], t_[maxn], p[maxn];
vector<int> v;
int head[maxn], cur[maxn], d[maxn];

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

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

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

void init()
{
    v.clear();
    mem(head, -1);
    cnt = 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 = Node[i].next)
        {
            node e = Node[i];
            if(!d[e.v] && e.c > 0)
            {
                d[e.v] = d[u] + 1;
                Q.push(e.v);
                if(e.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 = Node[i].next)
    {
        node e = Node[i];
        if(d[e.v] == d[u] + 1 && e.c > 0)
        {
            int V = dfs(e.v, min(cap, e.c));
            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 u)
{
    int ans = 0;
    while(bfs())
    {
        memcpy(cur, head, sizeof(head));
        ans += dfs(u, INF);
    }
    return ans;
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        init();
        s = 0, t = 400 + n + 1;
        int m_flow = 0;
        for(int i = 0; i < n; i++)
        {
            cin >> s_[i] >> p[i] >> e_[i] >> t_[i];
            m_flow += p[i] * t_[i];
            v.push_back(s_[i]);
            v.push_back(e_[i]);
        }
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < v.size() - 1; j++)
            {
                if(s_[i] <= v[j] && e_[i] >= v[j + 1])
                    add(400 + i + 1, j + 1, INF);
            }
        }
        for(int i = 0; i < n; i++)
        {
            add(s, 400 + i + 1, p[i] * t_[i]);
        }
        for(int j = 0; j < v.size() - 1; j++)
            add(j + 1, t, (v[j + 1] - v[j]) * m);
    //    cout << Dinic(s) << "   " << m_flow << endl;
        if(m_flow == Dinic(s))
            cout << "Yes" << endl;
        else
            cout << "No" << endl;

    }

    return 0;
}

 

转载于:https://www.cnblogs.com/WTSRUVF/p/9829976.html

你可能感兴趣的:(kebab HDU - 2883(按时间段建点))