您的位置:首页 > 其它

CodeForcesGym 100517H Hentium Scheduling

2015-10-10 19:30 429 查看

Hentium Scheduling

Time Limit: 2000ms
Memory Limit: 262144KB
This problem will be judged on CodeForcesGym. Original ID: 100517H
64-bit integer IO format: %I64d Java class name: (Any)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 210;
struct arc {
int to,next;
LL flow;
arc(int x = 0,LL y = 0,int z = -1) {
to = x;
flow = y;
next = z;
}
} e[2000010];
int head[maxn],d[maxn],cur[maxn],tot,S,T;
void add(int u,int v,LL flow) {
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,0,head[v]);
head[v] = tot++;
}
bool bfs() {
queue<int>q;
memset(d,-1,sizeof d);
d[S] = 1;
q.push(S);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] == -1) {
d[e[i].to] = d[u] + 1;
q.push(e[i].to);
}
}
}
return d[T] > -1;
}
LL dfs(int u,LL low) {
if(u == T) return low;
LL a,tmp = 0;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].flow && d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,min(low,e[i].flow)))) {
e[i].flow -= a;
e[i^1].flow += a;
low -= a;
tmp += a;
if(!low) break;
}
}
if(!tmp) d[u] = -1;
return tmp;
}
LL dinic(LL ret = 0) {
while(bfs()) {
memcpy(cur,head,sizeof head);
ret += dfs(S,INF);
}
return ret;
}
bool vis[maxn];
void dfs(int u) {
vis[u] = true;
for(int i = head[u]; ~i; i = e[i].next)
if(e[i].flow && !vis[e[i].to]) dfs(e[i].to);
}
int main() {
#define NAME "hentium"
freopen(NAME".in","r",stdin);
freopen(NAME".out","w",stdout);
int n,a,b;
while(scanf("%d",&n),n) {
memset(head,-1,sizeof head);
S = tot = 0;
T = n + 1;
for(int i = 1; i <= n; ++i) {
scanf("%d%d",&a,&b);
add(S,i,a);
add(i,T,b);
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j) {
scanf("%d",&a);
if(a) add(i,j,a);
}
printf("%I64d\n",dinic());
memset(vis,false,sizeof vis);
dfs(S);
for(int i = 1; i <= n; ++i)
printf("%d%c",vis[i]?2:1,i == n?'\n':' ');
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: