【模板】二分图最大权完美匹配
题目跳转
代码
速度快但不好记
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long long lll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
inline ll rd(){
ll x=0,f=1,ch=gc();
while(ch!=EOF&&(ch<'0'||ch>'9')){
if(ch=='-'){
f=-1;
}
ch=gc();
}
if(ch==EOF){
return x;
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch-'0');
ch=gc();
}
return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
inline void write1(lll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9){
write1(x/10);
}
pc(x%10+'0');
}
inline void write2(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9){
write2(x/10);
}
pc(x%10+'0');
}
const ll maxn=1e3+5,maxm=2.51e5+5;
const lll inf=1e18;
ll n,s,t,m,head[maxn],cost[maxn][maxn],match[maxn>>1],vec[maxn],pre[maxn];
bool vis[maxn],flow[maxn][maxn];
lll h[maxn],dis[maxn],ans2;
vector<ll> e[maxn];
queue<ll> q;
inline void adde(ll u,ll v,ll c){
flow[u][v]=1;
cost[u][v]=c;
e[u].emplace_back(v);
flow[v][u]=0;
cost[v][u]=-c;
e[v].emplace_back(u);
}
inline void spfa(){
for(ll i=0;i<=(n<<1)+1;i++){
h[i]=inf;
}
q.push(s);
vis[s]=true;
h[s]=0;
while(!q.empty()){
ll x=q.front();
q.pop();
vis[x]=false;
for(ll y:e[x]){
if(flow[x][y]&&h[y]>h[x]+cost[x][y]){
h[y]=h[x]+cost[x][y];
if(!vis[y]){
vis[y]=true;
q.push(y);
}
}
}
}
}
inline bool dijkstra(){
for(ll i=0;i<=(n<<1)+1;i++){
vis[i]=false;
dis[i]=inf;
}
dis[s]=0;
ll vcnt=0,x,xid;
for(ll i=0;i<=t;i++){
vec[++vcnt]=i;
}
for(ll cnt=0;cnt<=t;cnt++){
x=xid=-1;
for(ll j=1;j<=vcnt;j++){
if(x==-1||dis[vec[j]]<dis[x]){
x=vec[j];
xid=j;
if(dis[x]==0){
break;
}
}
}
if(x==-1||dis[x]>=inf){
break;
}
vis[x]=true;
vec[xid]=vec[vcnt--];
for(ll y:e[x]){
if(flow[x][y]&&dis[y]>dis[x]+cost[x][y]+h[x]-h[y]){
dis[y]=dis[x]+cost[x][y]+h[x]-h[y];
pre[y]=x;
}
}
}
return dis[t]!=inf;
}
int main(){
n=rd();
m=rd();
t=(n<<1)+1;
for(ll i=1;i<=m;i++){
ll u=rd(),v=rd(),c=rd();
adde(u,v+n,-c);
}
for(ll i=1;i<=n;i++){
adde(s,i,0);
adde(i+n,t,0);
}
spfa();
while(dijkstra()){
for(ll i=0;i<=(n<<1)+1;i++){
if(dis[i]!=inf){
h[i]+=dis[i];
}
}
ll now=t;
while(now!=s){
flow[pre[now]][now]=0;
flow[now][pre[now]]=1;
ans2+=cost[pre[now]][now];
now=pre[now];
}
}
for(ll i=n+1;i<=(n<<1);i++){
for(ll j:e[i]){
if(j!=t){
if(flow[i][j]){
match[i-n]=j;
break;
}
}
}
}
write1(-ans2);
pc('\n');
for(ll i=1;i<=n;i++){
write2(match[i]);
pc(' ');
}
fls();
return 0;
}
速度稍慢但好记(费用流)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
uint8_t buf[1<<20],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
ll rd(){
ll x=0,f=1,ch=gc();
while(ch!=EOF&&(ch<'0'||ch>'9')){
if(ch=='-'){
f=-1;
}
ch=gc();
}
if(ch==EOF){
return x;
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch-'0');
ch=gc();
}
return x*f;
}
uint8_t obuf[1<<20],*op=obuf;
#define fls() (fwrite(obuf,1,op-obuf,stdout),op=obuf,0)
#define pc(ch) (op==obuf+(1<<20)?fls():0,*op++=(ch))
void write(ll x){
if(x<0){
x=-x;
pc('-');
}
if(x>9){
write(x/10);
}
pc(x%10+'0');
}
const ll maxn=1e3+5,maxm=2.515e5+5,inf=1e18;
ll n,m,s,t,ecnt=1,head[maxn],dis[maxn],now[maxn],h[maxn],ans1,ans2,match[maxn>>1];
bool vis[maxn];
queue<ll> q;
priority_queue<pll,vector<pll>,greater<pll>> pq;
struct node{
ll nxt,to,lim,val;
}e[maxm<<1];
void adde(ll u,ll v,ll w,ll c){
e[++ecnt].nxt=head[u];
e[ecnt].to=v;
e[ecnt].lim=w;
e[ecnt].val=c;
head[u]=ecnt;
e[++ecnt].nxt=head[v];
e[ecnt].to=u;
e[ecnt].lim=0;
e[ecnt].val=-c;
head[v]=ecnt;
}
void spfa(){
for(ll i=0;i<=(n<<1)+1;i++){
h[i]=inf;
}
q.push(s);
vis[s]=true;
h[s]=0;
while(!q.empty()){
ll x=q.front();
q.pop();
vis[x]=false;
for(ll i=head[x];i;i=e[i].nxt){
ll y=e[i].to;
if(e[i].lim>0&&h[y]>h[x]+e[i].val){
h[y]=h[x]+e[i].val;
if(!vis[y]){
vis[y]=true;
q.push(y);
}
}
}
}
}
bool dijkstra(){
for(ll i=0;i<=(n<<1)+1;i++){
vis[i]=false;
dis[i]=inf;
}
pq=priority_queue<pll,vector<pll>,greater<pll>>();
pq.push({0,s});
dis[s]=0;
while(!pq.empty()){
ll x=pq.top().second;
pq.pop();
if(!vis[x]){
vis[x]=true;
for(ll i=head[x];i;i=e[i].nxt){
ll y=e[i].to;
if(e[i].lim>0&&dis[y]>dis[x]+e[i].val+h[x]-h[y]){
dis[y]=dis[x]+e[i].val+h[x]-h[y];
pq.push({dis[y],y});
}
}
}
}
return dis[t]!=inf;
}
ll dfs(ll x,ll sum){
if(x==t){
return sum;
}
ll res=0,k;
vis[x]=true;
for(ll i=now[x];i&∑i=e[i].nxt){
now[x]=i;
ll y=e[i].to;
if(e[i].lim>0&&!vis[y]&&h[y]==h[x]+e[i].val){
k=dfs(y,min(sum,e[i].lim));
e[i].lim-=k;
e[i^1].lim+=k;
sum-=k;
res+=k;
ans2+=k*e[i].val;
}
}
vis[x]=false;
return res;
}
int main(){
n=rd();
m=rd();
t=(n<<1)+1;
for(ll i=1;i<=m;i++){
ll u=rd(),v=rd(),c=rd();
adde(u,v+n,1,-c);
}
for(ll i=1;i<=n;i++){
adde(s,i,1,0);
adde(i+n,t,1,0);
}
spfa();
while(dijkstra()){
for(ll i=0;i<=(n<<1)+1;i++){
vis[i]=false;
now[i]=head[i];
if(dis[i]!=inf){
h[i]+=dis[i];
}
}
ans1+=dfs(s,inf);
}
write(-ans2);
pc('\n');
for(ll i=1;i<=m;i++){
ll id=i<<1;
if(e[id].lim==0){
match[e[id].to-n]=e[id^1].to;
}
}
for(ll i=1;i<=n;i++){
write(match[i]);
pc(' ');
}
fls();
return 0;
}
Goodbye!

浙公网安备 33010602011771号