本文共 1734 字,大约阅读时间需要 5 分钟。
突然发现自己在图论①被dalao吊着打。。。
看到数据范围1000,感觉可以直接枚举连边,然后新建两个点就好了。
#include #include #include #include #include #include #include #include #include using namespace std;#define ll long long#define re register#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)inline int gi(){ int f=1,sum=0;char ch=getchar(); while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();} return f*sum;}const int N=2010,M=3000010,Inf=1e9+10;int s=0,t,MaxFlow,MinCost,a,b;class Graph{private: int front[N],cnt,nxt[M<<1],to[M<<1],w[M<<1],c[M<<1],dis[N],vis[N],fa[N],from[N]; bool SPFA(){ queue Q;while(!Q.empty())Q.pop(); Q.push(s);memset(dis,127,sizeof(dis));vis[s]=1;dis[s]=0; while(!Q.empty()){ int u=Q.front();Q.pop();vis[u]=0; for(int i=front[u];i!=-1;i=nxt[i]){ int v=to[i]; if(w[i] && dis[v]>dis[u]+c[i]){ dis[v]=dis[u]+c[i];from[v]=i;fa[v]=u; if(!vis[v]){ vis[v]=1;Q.push(v); } } } } return dis[t+1]!=dis[t]; }public: void Add(int u,int v,int val,int f){to[cnt]=v;nxt[cnt]=front[u];front[u]=cnt;w[cnt]=val;c[cnt]=f;++cnt;} void init(){memset(front,-1,sizeof(front));cnt=0;} void Solve(){ while(SPFA()){ int d=Inf; for(int i=t;i!=s;i=fa[i])d=min(d,w[from[i]]); MaxFlow+=d;MinCost+=d*dis[t]; for(int i=t;i!=s;i=fa[i]){w[from[i]]-=d;w[from[i]^1]+=d;} } }}MfMc;int gcd(int a,int b){ if(!b)return a; return gcd(b,a%b);}int main(){ MfMc.init(); a=gi();b=gi();s=0;t=2*b+1; for(int i=a;i<=b;i++){ MfMc.Add(i+b,t,1,0);MfMc.Add(t,i+b,0,0); MfMc.Add(s,i,1,0);MfMc.Add(i,s,0,0); for(int j=a;j
转载于:https://www.cnblogs.com/mle-world/p/10256104.html