[] JSOI2009 team gains

Face questions

https://www.luogu.org/problem/P4307

answer

Japanese film out of $ aysn $.

Points to $ 2n + m $ bipartite graph approach can easily occur.

The point everyone split into success and failure points, then a person is to win a few games and all the people of his game and the number of field failures, limit traffic in the new point on the line.

More preferred approach, points, $ n + m $:

Adjustment Act.

Assume that everyone is lost, and then a game, win the person is wins +1, -1 defeat games, lose unchanged.

Such a seemingly put into a binary function of the membership function.

Then count the right side, according to a method of connecting directly above the edge on the line, without dismantling point.

#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 10050
#define INF 1000000007
#define S 0
#define T (n+m+1)
#define LL long long
#define ri register int
using namespace std;

int n,m;
int a[N],b[N],preb[N],c[N],d[N],e[N];
int u[N],v[N];

struct graph {
  vector<int> to,w,c;
  vector<int> ed[N];
  LL dis[N]; int cur[N];
  bool vis[N];
  void add_edge(int a,int b,int aw,int ac) {
    to.push_back(b); w.push_back(aw); c.push_back(ac);  ed[a].push_back(to.size()-1);
    to.push_back(a); w.push_back(0);  c.push_back(-ac); ed[b].push_back(to.size()-1);
  }
  bool spfa() {
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    queue<int> q;
    dis[S]=0;q.push(S);vis[S]=1;
    while (!q.empty()) {
      int x=q.front(); q.pop();
      for (ri i=0;i<ed[x].size();i++) {
        int e=ed[x][i];
        if (dis[to[e]]>dis[x]+c[e] && w[e]) {
          dis[to[e]]=dis[x]+c[e];
          if (!vis[to[e]]) vis[to[e]]=1,q.push(to[e]);
        }
      }
      vis[x]=0;
    }
    return dis[T]<INF;
  }
  int dfs(int x,int lim) {
    if (x==T || !lim) return lim;
    LL sum=0; vis[x]=1;
    for (ri &i=cur[x];i<ed[x].size();i++) {
      int e=ed[x][i];
      if (dis[x]+c[e]==dis[to[e]] && w[e] && !vis[to[e]]) {
        int f=dfs(to[e],min(lim,w[e]));
        w[e]-=f; w[1^e]+=f;
        lim-=f; sum+=f;
        if (!lim) return sum;
      }
    }
    return sum;
  }
  LL zkw() {
    LL ret=0;
    while (spfa()) {
      memset(vis,0,sizeof(vis));
      memset(cur,0,sizeof(cur));
      ret+=dfs(S,INF)*dis[T];
    }
    return ret;
  }
} G;

int main(){
  scanf("%d %d",&n,&m);
  for (ri i=1;i<=n;i++) {
    scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
    preb[i]=b[i];
  }
  for (ri i=1;i<=m;i++) {
    scanf("%d %d",&u[i],&v[i]);
    b[u[i]]++; b[v[i]]++;
  }
  for (ri i=1;i<=n;i++) e[i]=a[i]+b[i];
  for (ri i=1;i<=m;i++) {
    G.add_edge(S,i,1,0);
    G.add_edge(i,m+u[i],1,0);
    G.add_edge(i,m+v[i],1,0);
  }
  for (ri i=1;i<=n;i++) {
    for (ri j=a[i];j+preb[i]<e[i];j++) G.add_edge(m+i,T,1,2*j*c[i]+c[i]-2*d[i]*e[i]+2*j*d[i]+d[i]);
  }
  LL ans=0;
  for (ri i=1;i<=n;i++) ans+=b[i]*b[i]*d[i]+a[i]*a[i]*c[i];
  ans+=G.zkw();
  cout<<ans<<endl;
}

 

Guess you like

Origin www.cnblogs.com/shxnb666/p/11286174.html