看错题面,还写挂每个格子对应id的式子

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
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

int n,m,k,ans,tot=1,hd[(int)1e5+9],cur[(int)1e5+9],s,t,dep[(int)1e5+9];
pii dir[4]={{1,0},{0,1},{-1,0},{0,-1}};

struct Edge{
int to,nxt,v;
}e[(int)2e5+9];
void add(int u,int v,int w){
e[++tot]={v,hd[u],w},hd[u]=tot;
e[++tot]={u,hd[v],0},hd[v]=tot;
}

bool bfs(int st,int en){
for(int i=0;i<=t;++i)dep[i]=0;
queue<int>q;
memcpy(cur,hd,sizeof(hd));
q.push(st);
dep[st]=1;
while(!q.empty()){
auto u=q.front();
q.pop();
for(int eg=hd[u];eg;eg=e[eg].nxt){
if(!dep[e[eg].to]&&e[eg].v>0){
dep[e[eg].to]=dep[u]+1;
q.push(e[eg].to);
}
}
}
return !!dep[en];
}

int dfs(int u,int en,int flow){
if(u==en)return flow;
int r=flow;
for(int eg=cur[u];eg&&r;eg=e[eg].nxt){
cur[u]=eg;
if(e[eg].v>0&&dep[e[eg].to]==dep[u]+1){
int c=dfs(e[eg].to,en,min(r,e[eg].v));
r-=c;
e[eg].v-=c,e[eg^1].v+=c;
}
}
return flow-r;
}

int dinic(int st,int en){
int ret=0;
while(bfs(st,en)){
ret+=dfs(st,en,1<<29);
}
return ret;
}

inline int id(int x,int y){return (x-1)*m+y;}

signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
s=n*m+1,t=n*m+2;
for(int i=1,x;i<=n;++i)for(int j=1;j<=m;++j){
cin>>x;
if(x==1)add(s,id(i,j),1<<29);
else if(x==2)add(id(i,j),t,1<<29);
for(auto [xx,yy]:dir){
xx+=i,yy+=j;
if(xx<=n&&xx>=1&&yy<=m&&yy>=1){
add(id(i,j),id(xx,yy),1);
}
}
}
cout<<dinic(s,t);
return 0;
}

老是看错题。。。。

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
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n,c[209],a[209],ss,tt,ans;
int hd[(int)1e5+9],cur[(int)1e5+9],tot=1,dep[(int)1e5+9];

struct Edge{
int to,nxt,v;
}e[(int)5e5+9];
void add(int u,int v,int w){
e[++tot]={v,hd[u],w},hd[u]=tot;
e[++tot]={u,hd[v],0ll},hd[v]=tot;
}

vi g[209],dis[209][209];
bitset<209> vis;

void ddfs(int u,int d,int rt){
vis[u]=1,dis[rt][d].push_back(u);
for(auto it:g[u]){
if(!vis[it])ddfs(it,d+1,rt);
}
}


bool bfs(int st,int en){
for(int i=0;i<=tt;++i)dep[i]=0;
queue<int>q;
memcpy(cur,hd,sizeof(hd));
q.push(st);
dep[st]=1;
while(!q.empty()){
auto u=q.front();
q.pop();
for(int eg=hd[u];eg;eg=e[eg].nxt){
if(!dep[e[eg].to]&&e[eg].v>0){
dep[e[eg].to]=dep[u]+1;
q.push(e[eg].to);
}
}
}
return !!dep[en];
}

int dfs(int u,int en,int flow){
if(u==en)return flow;
int r=flow;
for(int eg=cur[u];eg&&r;eg=e[eg].nxt){
cur[u]=eg;
if(e[eg].v>0&&dep[e[eg].to]==dep[u]+1){
int c=dfs(e[eg].to,en,min(r,e[eg].v));
r-=c;
e[eg].v-=c,e[eg^1].v+=c;
}
}
return flow-r;
}

int dinic(int st,int en){
int ret=0;
while(bfs(st,en)){
ret+=dfs(st,en,0x3f3f3f3f);
}
return ret;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
ss=n*n+n+1,tt=ss+1;
for(int i=1;i<=n;++i){
cin>>c[i];
add(i+n*n,tt,c[i]);
}
for(int i=1;i<=n;++i)cin>>a[i];
for(int i=1,x,y;i<n;++i){
cin>>x>>y;
g[x].push_back(y),g[y].push_back(x);
}
for(int i=1;i<=n;++i){
vis.reset();
ddfs(i,0,i);
}
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
add(ss,(i-1)*n+j,a[j]-a[j-1]);
if(j>1)add((i-1)*n+j,(i-1)*n+j-1,0x3f3f3f3f);
for(auto it:dis[i][j-1])add((i-1)*n+j,n*n+it,0x3f3f3f3f);
}
}
ans=n*a[n]-dinic(ss,tt);
cout<<ans;
return 0;
}

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
139
140
141
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

template <class T> inline T read(const T sample) {
T x=0; int f=1; char s;
while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
return x*f;
}

ll n,m;
string s;
int hd[1009],cur[1009],dep[1009],tot=1,ss,tt,ans;
bitset<1009> vis;
struct Edge{
int to,nxt,v;
}e[(int)2e5+9];
void add(int u,int v,int w){
e[++tot]={v,hd[u],w},hd[u]=tot;
e[++tot]={u,hd[v],0},hd[v]=tot;
}

void dfs(int u){
vis[u]=1;
for(int eg=hd[u];eg;eg=e[eg].nxt){
if(!vis[e[eg].to]&&e[eg].v>0)dfs(e[eg].to);
}
}

bool bfs(int st,int en){
for(int i=0;i<=n+m+2;++i)dep[i]=0;
queue<int>q;
memcpy(cur,hd,sizeof(hd));
q.push(st);
dep[st]=1;
while(!q.empty()){
auto u=q.front();
q.pop();
for(int eg=hd[u];eg;eg=e[eg].nxt){
if(!dep[e[eg].to]&&e[eg].v>0){
dep[e[eg].to]=dep[u]+1;
q.push(e[eg].to);
}
}
}
return !!dep[en];
}

int dfs(int u,int en,int flow){
if(u==en)return flow;
int r=flow;
for(int eg=cur[u];eg&&r;eg=e[eg].nxt){
cur[u]=eg;
if(e[eg].v>0&&dep[e[eg].to]==dep[u]+1){
int c=dfs(e[eg].to,en,min(r,e[eg].v));
r-=c;
e[eg].v-=c,e[eg^1].v+=c;
}
}
return flow-r;
}

int dinic(int st,int en){
int ret=0;
while(bfs(st,en)){
ret+=dfs(st,en,0x3f3f3f3f);
}
return ret;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>m>>n;
ss=n+m+1,tt=ss+1;
//while(getchar()!='\n');
getline(cin,s);
for(int i=1;i<=m;++i){
vi v;
getline(cin,s);
ll x=0,f=1,lst=0;
for(auto it:s){
if(lst){
if(it>='0'&&it<='9')x=(x<<1)+(x<<3)+(it^48),lst=1;
else {
v.push_back(f*x);
x=0,f=1,lst=0;
}
}else{
if(it=='-')f=-1,lst=1;
else if(it>='0'&&it<='9')x=(x<<1)+(x<<3)+(it^48),lst=1;
else lst=0;
}
}
if(lst)v.push_back(f*x);
add(ss,i,v[0]);
ans+=v[0];
for(int j=1;j<v.size();++j)add(i,m+v[j],0x3f3f3f3f);
}
for(int i=1,x;i<=n;++i){
cin>>x;
add(m+i,tt,x);
}

ans-=dinic(ss,tt);
dfs(ss);
f(i,1,m)if(vis[i])cout<<i<<" ";
cout<<"\n";
f(i,m+1,n+m)if(vis[i])cout<<i-m<<' ';
cout<<"\n";
cout<<ans<<"\n";
return 0;
}

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
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n,m,a[(int)4e5+9],l[(int)4e5+9],r[(int)4e5+9],ans;
bitset<(int)5e5+9> vis;
priority_queue<pair<ll,ll>>q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
f(i,1,n)cin>>a[i];
if(2*m>n){
cout<<"Error!\n";
return 0;
}
for(int i=1;i<=n;++i)l[i]=(n+i-2)%n+1,r[i]=(n+i)%n+1;
for(int i=1;i<=n;++i)q.push({a[i],i});
while(!q.empty()&&m){
auto [aa,bb]=q.top();
q.pop();
if(vis[bb])continue;
vis[bb]=vis[l[bb]]=vis[r[bb]]=1,ans+=aa,n+=1,m-=1,a[n]=a[l[bb]]+a[r[bb]]-a[bb];
l[n]=l[l[bb]],r[n]=r[r[bb]],r[l[n]]=n,l[r[n]]=n;
q.push({a[n],n});
}
cout<<ans;
return 0;
}


建虚点,注意左右边界要更新

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
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n,k,l[(int)1e6+9],r[(int)1e6+9],ans,a[(int)1e6+9];
priority_queue<pair<ll,ll>> q;
bitset<(int)1e6+9> vis;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;++i)l[i]=i-1,r[i]=i+1;
for(ll i=1,x;i<=n;++i){
cin>>a[i];
q.push({a[i],i});
}
while(k&&!q.empty()&&q.top().first>0){
auto [aa,bb]=q.top();
q.pop();
if(vis[bb])continue;
vis[bb]=vis[l[bb]]=vis[r[bb]]=1,ans+=aa,n+=1,k-=1,a[n]=a[l[bb]]+a[r[bb]]-a[bb];
l[n]=l[l[bb]],r[n]=r[r[bb]],r[l[n]]=n,l[r[n]]=n;
q.push({a[n],n});
}
cout<<ans;
return 0;
}
0%