和POJ3422一樣
刪掉K把匯點與源點的容量改為2(因為有兩個方向的選擇)即可
#include#include #include #include #include #include const int maxn = 20000; const int maxm = 800000; const int inf = 1e8; const int INF = 0x3f3f3f3f; #define MIN INT_MIN #define MAX 1e6 #define LL long long #define init(a) memset(a,0,sizeof(a)) #define FOR(i,a,b) for(int i = a;ib)?(a):(b) #define min(a,b) (a>b)?(b):(a) using namespace std; struct node { int u,v,w,cap,next; } edge[maxm]; int pre[maxn],dis[maxn],head[maxn],cnt; bool vis[maxn]; int n; void add(int u,int v,int c,int cap) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].w=c; edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].u=v; edge[cnt].v=u; edge[cnt].w=-c; edge[cnt].cap=0; edge[cnt].next=head[v]; head[v]=cnt++; } int spfa(int s,int t) { queue q; while(q.empty()==false) q.pop(); q.push(s); memset(vis,0,sizeof(vis)); memset(pre,-1,sizeof(pre)); FOR(i,s,t+1) dis[i] = -1;//求最長路dis數組初始化為-1 dis[s]=0; vis[s] = 1; while(!q.empty()) { int u=q.front(); q.pop(); vis[u] = 0; for(int i=head[u]; i!=-1; i=edge[i].next) { if(edge[i].cap && dis[edge[i].v] < (dis[u]+edge[i].w))//求最長路 { dis[edge[i].v] = dis[u]+edge[i].w; pre[edge[i].v] = i; if(!vis[edge[i].v]) { vis[edge[i].v]=1; q.push(edge[i].v); } } } } if(dis[t] != -1)//------------------忘改了。。 return 1; else return 0; } int MinCostMaxFlow(int s,int t) { int flow=0,cost=0; while(spfa(s,t)) { int df = inf; for(int i = pre[t]; i!=-1; i=pre[edge[i].u]) { if(edge[i].cap