1 5 6 2 2 1 2 2 1 3 1 1 1 2 3 1 1 2 1 2
3 0 2 1HintNo city was rebuilt in the third year,city 1 and city 3 were rebuilt in the fourth year,and city 2 was rebuilt in the sixth year.
费用流
二分图,左边m次操作,右边n个城市,求最优匹配
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (1000+10) #define MAXM (100000*4+10) #define MAXT (50+10) #define MAXn (200+10) #define MAXm (500+10) #define MAXk (200+10) #define eps (1e-3) long long mul(long long a,long long b){return (a*b)%F;} long long add(long long a,long long b){return (a+b)%F;} long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;} typedef long long ll; class Cost_Flow { public: int n,s,t; int q[10000]; int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size; double cost[MAXM]; void addedge(int u,int v,int w,double c) { edge[++size]=v; weight[size]=w; cost[size]=c; next[size]=pre[u]; pre[u]=size; } void addedge2(int u,int v,int w,double c){addedge(u,v,w,c),addedge(v,u,0,-c);} bool b[MAXN]; double d[MAXN]; int pr[MAXN],ed[MAXN]; bool SPFA(int s,int t) { For(i,n) d[i]=INF; MEM(b) d[q[1]=s]=0;b[s]=1; int head=1,tail=1; while (head<=tail) { int now=q[head++]; Forp(now) { int &v=edge[p]; if (weight[p]&&d[now]+cost[p]<d[v]) { d[v]=d[now]+cost[p]; if (!b[v]) b[v]=1,q[++tail]=v; pr[v]=now,ed[v]=p; } } b[now]=0; } return fabs(d[t]-INF)>eps; } double totcost; double CostFlow(int s,int t) { while (SPFA(s,t)) { int flow=INF; for(int x=t;x^s;x=pr[x]) flow=min(flow,weight[ed[x]]); totcost+=(double)flow*d[t]; for(int x=t;x^s;x=pr[x]) weight[ed[x]]-=flow,weight[ed[x]^1]+=flow; } return totcost; } void mem(int n,int t) { (*this).n=n; size=1; totcost=0; MEM(pre) MEM(next) } }S; int n,m,k; bool b[MAXn][MAXn]; int path[MAXm],cnt; bool vis[MAXn]; void dfs(int x) { vis[x]=1; For(i,n) if (b[x][i] && (!vis[i])) { dfs(i); } } int main() { // freopen("J.in","r",stdin); // freopen(".out","w",stdout); int T;cin>>T; while(T--) { MEM(b) MEM(path) cnt=0; cin>>n>>m>>k; S.mem(n+m+2,n+m+2); For(i,n) S.addedge2(1+m+i,n+m+2,1,0); For(i,m) { int p;scanf("%d",&p); if (p==1) { int x; scanf("%d",&x); path[++cnt]=S.size+2; S.addedge2(1,i+1,k,(double)m-i); MEM(vis) dfs(x); For(j,n) if ( vis[j]) S.addedge2(i+1,1+m+j,1, 0 ); } else if (p==2) { int x,y; scanf("%d%d",&x,&y); b[x][y]=b[y][x]=1; } else { int l; scanf("%d",&l); while (l--) { int x,y; scanf("%d%d",&x,&y); b[x][y]=b[y][x]=0; } } } S.CostFlow(1,n+m+2); // Fork(i,2,S.size) cout<<S.weight[i]<<' '<<S.cost[i]<<endl; int ans=0; For(i,cnt) ans+=S.weight[path[i]]; printf("%d\n",ans); For(i,cnt-1) printf("%d ",S.weight[path[i]]); printf("%d\n",S.weight[path[cnt]]); } return 0; }