QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#275640 | #6870. Coin | anhduc2701 | AC ✓ | 416ms | 8072kb | C++23 | 2.9kb | 2023-12-04 21:59:38 | 2023-12-04 21:59:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
int n,m,k;
struct edge{
int u,v,f,c;
edge(){}
edge(int _u,int _v,int _c){
u=_u;
v=_v;
c=_c;
f=0;
}
};
struct Dinic{
vector<edge>ed;
vector<vector<int>>G;
vector<int>level;
vector<int>cd;
int n,source,sink;
void init(int _n,int _source,int _sink){
n=_n;
source=_source;
sink=_sink;
cd.assign(n+2,0);
level.assign(n+2,0);
G.assign(n+2,vector<int>());
}
void addedge(int u,int v,int c){
G[u].pb(ed.size());
ed.pb(edge(u,v,c));
G[v].pb(ed.size());
ed.pb(edge(v,u,0));
}
bool bfs(){
fill(level.begin(),level.end(),-1);
level[source]=0;
queue<int>qu;
qu.push(source);
while(qu.size()){
int u=qu.front();
qu.pop();
for(auto id:G[u]){
if(level[ed[id].v]!=-1 || ed[id].c-ed[id].f<1)continue;
level[ed[id].v]=level[u]+1;
qu.push(ed[id].v);
}
}
if(level[sink]!=-1)return true;
return false;
}
int dfs(int u,int cap){
//cout << u <<' '<<cap<< '\n';
if(u==sink || cap==0)return cap;
for(int&i =cd[u];i<G[u].size();i++){
int id=G[u][i];
if(level[ed[id].v]!=level[u]+1 || ed[id].c-ed[id].f<1)continue;
int c=dfs(ed[id].v,min(cap,ed[id].c-ed[id].f));
if(c==0)continue;
ed[id].f+=c;
ed[id^1].f-=c;
return c;
}
return 0;
}
int Flow(){
int f=0;
while(bfs()){
fill(cd.begin(),cd.end(),0);
while(int pushed=dfs(source,1e9)){
f+=pushed;
}
}
return f;
}
};
int a[3005];
int limit[3005];
int last[3005];
void solve(){
cin >> n >> m >> k;
int source=0;
int sink=n+m*4+1;
Dinic A;
A.init(sink,source,sink);
for(int i=1;i<=n;i++){
cin >> limit[i];
A.addedge(source,i,1);
last[i]=i;
}
int cnt=n;
for(int i=1;i<=m;i++){
int vira=++cnt;
int virb=++cnt;
int a,b;
cin >> a >> b;
A.addedge(last[a],virb,1);
A.addedge(last[b],vira,1);
A.addedge(last[a],vira,1e9);
A.addedge(last[b],virb,1e9);
last[a]=++cnt;
last[b]=++cnt;
A.addedge(vira,last[a],limit[a]);
A.addedge(virb,last[b],limit[b]);
}
for(int i=1;i<=k;i++){
int x; cin >> x;
A.addedge(last[x],sink,1e9);
}
cout << A.Flow() << '\n';
}
signed main(){
// freopen("input.inp","r",stdin);
cin.tie(0),cout.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while(t--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 416ms
memory: 8072kb
input:
10 2621 3000 873 542 5 3 3 1 2 8 2 2 4 1 1 2 5 3 1 5 1 2 4 3 1 1 6 3 3 3 1 1 1 1 5 6 2 4 2 2 2 1 5 2 1 3 3 2 2 1 3 1 1 4 3 3 2 4 2 2 1 1 3 1 1 1 2 2 1 2 3 3 2 1 1 5 1 3 3 1 1 3 2 2 2 3 4 4 4 3 1 1 1 1 2 1 1 1 1 1 1 4 2 2 2 2 1 6 2 3 1 3 2 3 1 3 2 1 2 1 3 1 2 1 6 1 3 1 3 2 1 2 2 2 2 4 3 1 3 1 4 1 2 1...
output:
1584 1441 1691 1673 1444 1481 1573 1399 1839 1788
result:
ok 10 lines