Ural 1774 Barber of the Army of Mages 最大流

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1774

1774. Barber of the Army of Mages

Time limit: 0.5 second
Memory limit: 64 MB
Petr, elected as a warlord of the army of mages, faced a challenging problem. All magicians recruited in the army had heavy beards, which were quite unacceptable for soldiers. Therefore, Petr ordered all recruits to shave their beards as soon as possible. Of course, all magicians refused to do it, referring to the fact they don't know any shaving spell. Fortunately, a magician Barberian agreed to shave all recruits.
Barberian can cast a “Fusion Power” spell which shaves beards of at most  k magicians in one minute. In order to achieve full effect every magician should be shaved twice: the first spell shaves close, the second spell shaves even closer. For each recruit Petr appointed a time when he should visit Barberian. Unfortunately, the discipline in the new army is still far from perfect, so every magician will come to Barberian in time, but everyone will wait for the shave until his patience is exhausted and will disappear after that.
Determine whether Barberian will be able to shave beards of all magicians before they disappear.

Input

The first line contains two space-separated integers  n and  k (1 ≤  nk ≤ 100), which are the number of recruits in the army and the number of magicians Barber can shave simultaneously. The  i-th of the following  n lines contains space-separated integers  ti and  si (0 ≤  ti ≤ 1000; 2 ≤  si ≤ 1000), which are the time in minutes, at which the  i-th magician must come to Barberian, and the time in minutes he is ready to spend there, including shaving time.

Output

If Barberian is able to shave beards of all magicians, output “Yes” in the first line. The  i-th of the following  n lines should contain a pair of integers  piqi, which are the moments at which Barberian should cast the spell on the  i-th magician( ti ≤  pi <  qi ≤  ti +  si − 1). If at least one magician disappears before being completely shaved, output a single word “No”.

Samples

inputoutput
3 2
1 3
1 3
1 3

Yes
1 2
1 3
2 3
2 1
1 3
1 3
No

 

题意

有很多人要剃胡子,有个很神奇的理发师,可以在每分钟给k个人护理。每个人必须被护理两次。给你每个人进入理发店的时间和耐心,输出在什么时候给这些人剪胡子。

题解

发现时间只有1000,那么可以每一分钟建立一个节点,然后连到人的节点上,容量为1,表示每个人1分钟最多被护理一次;每个人连接一条边到T,容量为2,表示每个人需要被护理两次;从S连接容量为k的边到每个时间,表示每一分钟可以处理k个人。然后跑一发Dinic,最后在残余网络上找解就好。详见代码:

代码

#include<iostream>
#include<stack>
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#define MAX_V 4567
#define MAX_N 10004
#define INF 2500005
using namespace std;

struct edge{int to,cap,rev;bool isRev;};

vector<edge> G[MAX_N];
int level[MAX_V];
int iter[MAX_V];

void add_edge(int from,int to,int cap) {
    G[from].push_back((edge) {to, cap, G[to].size(),0});
    G[to].push_back((edge) {from, 0, G[from].size() - 1,1});
}

void bfs(int s) {
    memset(level, -1, sizeof(level));
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (int i = 0; i < G[v].size(); i++) {
            edge &e = G[v][i];
            if (e.cap > 0 && level[e.to] < 0) {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v,int t,int f) {
    if (v == t)return f;
    for (int &i = iter[v]; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[v] < level[e.to]) {
            int d = dfs(e.to, t, min(f, e.cap));
            if (d > 0) {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t) {
    int flow = 0;
    for (; ;) {
        bfs(s);
        if (level[t] < 0)return flow;
        memset(iter, 0, sizeof(iter));
        int f;
        while ((f = dfs(s, t, INF)) > 0) {
            flow += f;
        }
    }
}

int n,k;
int t[MAX_N],r[MAX_N];
int m=2222;

int S=3232;
int T=3332;

int ans[MAX_N][2];

int main() {
    scanf("%d%d", &n, &k);
    memset(ans, -1, sizeof(ans));
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &t[i], &r[i]);
        for (int j = 0; j < r[i]; j++)
            add_edge(j + t[i], i + m, 1);
    }
    for (int i = 0; i < m; i++)add_edge(S, i, k);
    for (int i = 0; i < n; i++)add_edge(i + m, T, 2);

    int f = max_flow(S, T);
    if (f != 2 * n) {
        cout << "No" << endl;
        return 0;
    }
    printf("Yes\n");
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < G[i].size(); j++) {
            if(G[i][j].isRev||(G[i][j].cap==1))continue;
            int u = G[i][j].to - m;
            if (ans[u][0] == -1)ans[u][0] = i;
            else if (ans[u][1] == -1)ans[u][1] = i;
        }
    }
    for (int i = 0; i < n; i++)
        printf("%d %d\n", ans[i][0], ans[i][1]);
    return 0;
}

 

转载于:https://www.cnblogs.com/HarryGuo2012/p/4728167.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值