zoukankan      html  css  js  c++  java
  • 【NOI2008】志愿者招募

    题面

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

    题解

    套路作差,每个点代表一组等式,然后连边。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<queue>
    #include<vector>
    #include<algorithm>
    #define ri register int
    #define N 1050
    #define S (n+1)
    #define T (n+2)
    #define INF 1000000007
    #define LL long long
    
    using namespace std;
    
    int n,m;
    int need[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,0x2f,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() {
      int s,t,c;
      scanf("%d %d",&n,&m);
      for (ri i=1;i<=n;i++) {
        scanf("%d",&need[i]);
      }
      for (ri i=0;i<=n;i++) {
        if (need[i]-need[i+1]>0) G.add_edge(S,i,need[i]-need[i+1],0);
        else if (need[i]-need[i+1]<0) G.add_edge(i,T,need[i+1]-need[i],0);
        if (i<n) G.add_edge(i,i+1,INF,0);
      }
      for (ri i=1;i<=m;i++) {
        scanf("%d %d %d",&s,&t,&c);
        G.add_edge(t,s-1,INF,c);
      }
      cout<<G.zkw()<<endl;
    }
  • 相关阅读:
    wxpython快速入门
    python核心编程 第四章 和第五章
    python核心编程 第三章
    python核心编程 第二章 快速入门
    Nginx 使用札记
    PHP 函数总结
    node.js安装部署
    linux
    在Linux上安装Git
    php超级全局变量
  • 原文地址:https://www.cnblogs.com/shxnb666/p/11285894.html
Copyright © 2011-2022 走看看