| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
虽然我看了很久大神怎么做的。但是。我做了点改善。。。plus不喜欢临接矩阵#include <stdio.h>
#include<string.h>
#include <iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#define INFINITE 0x3f3f3f3f
#define MAX_M 1000 + 5
#define MAX_N 100 + 10
int PigHouse[MAX_M];
int Pre[MAX_M];
int G[MAX_N][MAX_N];
int Visit[MAX_N];
int M, N;
int ss, tt;
using namespace std;
void initVar()
{
memset((char *)&PigHouse, 0, sizeof(PigHouse));
memset((char *)&Pre, 0, sizeof(Pre));
memset((char *)&G, 0, sizeof(G));
memset((char *)&Visit, 0, sizeof(Visit));
}
void addEdge(int from, int to, int weight)
{
// printf("addEdge from = %d, to = %d, weight = %d\n", from, to, weight);
G[from][to] += weight;
}
void printfMatix()
{
int i, j;
for(i = 1; i<= N+2; i++)
{
for(j = 1; j<= N+2; j++)
{
printf("%11d", G[i][j]);
}
printf("\n");
}
printf("end\n");
}
bool makediv()
{
// printf("makediv\n");
memset((char *)&Visit, 0, sizeof(Visit));
queue<int> q;
q.push(ss);
Visit[ss] = 1;
// printf("Visit[%d] = %d\n", ss, Visit[ss]);
while(q.size())
{
int now = q.front();
q.pop();
if(now == tt)
return true;
for(int i = 1; i<=tt; i++)
{
if(0 == Visit[i] && G[now][i]>0 )
{
Visit[i] = Visit[now] + 1;
// printf("now = %d, Visit[%d] = %d\n",now, i, Visit[i]);
q.push(i);
}
}
}
return false;
}
int min(int a, int b)
{
if(a<b)
return a;
else
return b;
}
int dfs(int from, int to, int maxflow)
{
// printf("from = %d, to = %d, maxflow = %d\n", from, to, maxflow);
if(from == to)
{
// printf("reach the end maxflow = %d\n", maxflow);
return maxflow;
}
int ret = 0, flow = 0;
for(int i = 1; i<=tt; i++)
{
// printf("test from = %d, i = %d, to = %d, Visit[from] = %d, Visit[i] = %d\n", from, i, to, Visit[from], Visit[i]);
if(Visit[i] == Visit[from] + 1 && G[from][i]>0 )
{
// printf("from = %d, to = %d, i = %d, G[from][i] = %d, maxflow = %d, ret = %d \n", from, to, i, G[from][i], maxflow, ret);
flow = dfs(i, to, min(G[from][i], maxflow - ret) );
G[from][i] -= flow;
G[i][from] += flow;
ret = ret +flow;
// printf("why from = %d, to = %d flow = %d, ret = %d\n", from, to, flow, ret);
if(ret == maxflow)
return maxflow;
}
}
return ret;
}
void dinic()
{
int ans = 0;
//printf("dinic\n");
while( makediv() )
{
ans += dfs(ss, tt, INFINITE );
}
printf("%d\n", ans);
}
int main()
{
int i, j, k;
int key, want;
while(scanf("%d %d",&M, &N)!=EOF)
{
// printf("M = %d, N = %d\n");
initVar();
for(i = 1; i<=M; i++)
{
scanf("%d", &PigHouse[i]);
// printf("i = %d, PigHouse[i] = %d\n",i, PigHouse[i]);
}
ss = N + 1;
tt = N + 2;
// printf("ss = %d, tt = %d\n", ss, tt);
for(i = 1; i<=N; i++)
{
scanf("%d", &k);
// printf("the i = %d customers has %d keys\n", i, k );
for(j = 1; j<=k; j++)
{
scanf("%d", &key);
// printf("j = %d, key = %d, Pre[key] = %d ", j, key, Pre[key]);
if(Pre[key] == 0)
{
addEdge(ss, i, PigHouse[key]);
}else
{
addEdge(Pre[key], i, INFINITE);
}
Pre[key] = i;
}
scanf("%d", &want);
// printf("the %d customer want %d pig\n", i, want);
addEdge(i, tt, want);
}
// printfMatix();
dinic();
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator