书海扬帆的博客

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long ll;
const ll maxn=50050,maxm=500050,inf=2147483647;
struct node{
ll v,c,cost;
node *next,*rev;
}*pree[maxn],*h[maxn],pool[maxm];
ll n,s,p,m,day,f,a[maxn],sum;
ll tot=0;
ll src,sink;
inline void addEdge(ll u,ll v,ll c,ll cost){
node *p=&pool[++tot],*q=&pool[++tot];
p->v=v;p->c=c;p->cost=cost;p->next=h[u];h[u]=p;p->rev=q;
q->v=u;q->c=0;q->cost=-cost;q->next=h[v];h[v]=q;q->rev=p;
}
ll dis[maxn],q[maxn],pre_v[maxn];bool vis[maxn];
/*
inline bool SPFA(){
fill(dis+1,dis+n+1,inf);
memset(vis,false,sizeof(vis));
queue<ll> Q;
vis[src]=1,dis[src]=0;
Q.push(src);
while(!Q.empty()){
ll front=Q.front();
Q.pop();
vis[front]=false;
for(node *p=h[front];p;p=p->next){
if(p->c && dis[p->v]>dis[front]+p->cost){
dis[p->v]=dis[front]+p->cost;
pre_v[p->v]=front;
pree[p->v]=p;
if(!vis[p->v]){
vis[p->v]=1;
Q.push(p->v);
}
}
}
}
return dis[sink]!=inf;
}*/
inline bool spfa(){
for(ll i=1;i<maxn;i++) dis[i]=inf;
for(ll i=1;i<maxn;i++) vis[i]=0;
ll front=0,rear=0;
vis[src]=1,dis[src]=0;
q[rear++]=src;
while(front<rear)
{
ll u=q[front++];
vis[u]=0;
for(node *p=h[u];p;p=p->next)
{
if(p->c>0 && dis[p->v]>dis[u]+p->cost)
{
dis[p->v]=dis[u]+p->cost;
pre_v[p->v]=u;
pree[p->v]=p;
if(!vis[p->v])
{
vis[p->v]=1;
q[rear++]=p->v;
}
}
}
}
if(dis[sink]<inf) return true;
return false;
}

inline ll Update(){
ll u=sink,ret=inf;
while(u!=src){
ret=min(ret,pree[u]->c);
u=pre_v[u];
}
u=sink;
while(u!=src){
pree[u]->c-=ret;
pree[u]->rev->c+=ret;
u=pre_v[u];
}
return ret;
}
ll MaxFlow,Mincostflow;
inline void MinCostFlow(){
ll ret=0;
while(spfa()){
ret=Update();
MaxFlow+=ret;
Mincostflow+=ret*dis[sink];
}
}
inline ll read() {
ll x = 0, f = 1; char ch = getchar();
while (ch<'0' || ch>'9') {
if (ch == '-')f = -1;
ch = getchar();
}
while (ch >= '0'&&ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return f * x;
}
inline void write(ll x) {
ll y = 10, len = 1;
while (y <= x) {
y *= 10; len++;
}
while (len--) {
y /= 10; putchar(x / y + 48);x %= y;
}
}
int main(){
day=read();
for(ll i=1;i<=day;i++) a[i]=read();
p=read();m=read();f=read();n=read();s=read();
src=0,sink=2*day+1;
for(ll i=1;i<=day;i++){
addEdge(src,i,a[i],0);
addEdge(day+i,sink,a[i],0);
}
for(ll i=1;i<=day;i++){
if(i+1<=day) addEdge(i,i+1,inf,0);
if(i+m<=day) addEdge(i,day+i+m,inf,f);
if(i+n<=day) addEdge(i,day+i+n,inf,s);
addEdge(src,day+i,inf,p);
}
MinCostFlow();
write(Mincostflow);
return 0;
}

最大流算法,跑Dinic,建图是关键。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100010,maxm=200010;
const int INF=2147483647;
struct node{
int v,c;
node *next,*reverse;
}pool[maxm],*h[maxn];
int tot=0,level[maxn],q[maxn];
int n,m,src,sink;
int front,rear;
int people[maxn],capacity[maxn];
void addEdge(int u,int v,int c){
//cout<<u<<' '<<v<<' '<<c<<endl;
node *p=&pool[++tot],*q=&pool[++tot];
p->v=v;p->c=c;p->next=h[u];h[u]=p;p->reverse=q;
q->v=u;q->c=0;q->next=h[v];h[v]=q;q->reverse=p;
}
inline bool bfs(){
front=0,rear=1;
memset(level,-1,sizeof(level));
q[front]=src;level[src]=0;
while(front<rear){
int tmp=q[front++];
for(node *p=h[tmp];p;p=p->next){
if(level[p->v]<0 && p->c){
level[p->v]=level[tmp]+1;q[rear++]=p->v;
}
}
if(level[sink]>0) return true;
}
return false;
}
inline int find(int u,int key){
if(u==sink) return key;
int ret=0;
for(node *p=h[u];p;p=p->next){
if(p->c && level[p->v] == level[u]+1){
int tmp=find(p->v,min(key,p->c));
if(tmp){
ret+=tmp;
p->c-=tmp;
p->reverse->c+=tmp;
key-=tmp;
if(key<=0) break;
}
}
}
if(ret==0) level[u]=-1;
return ret;
}
inline int dinic(){
int totflow=0;
while(bfs()) totflow+=find(src,INF);
return totflow;
}
int main(){
int tot=0;
scanf("%d%d",&m,&n);
src=0;
sink=n+m+1;
//printf("src=%d;sink=%d\n",src,sink);
for(int i=1;i<=m;i++)
{
scanf("%d",&people[i]);
tot+=people[i];
//printf("tot=%d\n",tot);
addEdge(src,i,people[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&capacity[i]);
addEdge(i+m,sink,capacity[i]);
}
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
addEdge(i,j+m,1);
int res=dinic();
//printf("dinic()=%d\n",res);
if(tot!=res)
{
printf("0\n");
goto end;
}
else printf("1\n");
for(int i=1;i<=m;i++)
{
for(node *p=h[i];p;p=p->next)
{
if(p->c==0 && p->v!=sink)
{
printf("%d ",p->v-m);
}
}
printf("\n");
}
end:
return 0;
}



本站使用 Material-X 作为主题 , 总访问量为 次 。
载入天数...载入时分秒... 字数统计:725.9k