Intervals—-最小費用流

來源:互聯網
上載者:User

題目:http://poj.org/problem?id=3680

還是建圖的問題。

離散化+最小費用流。

推薦博文:http://hi.baidu.com/juner_king/blog/item/a6b7cc822cba01dd9023d933.html

原始碼:(用時:547ms)

#include <stdio.h>#include <string.h>#include <algorithm>using namespace  std;typedef int typef;typedef int typec;const int N = 405, M = 2000;const typef inff = 0x3f3f3f3f;const typec infc = 0x3f3f3f3f;int a[N],b[N];int l[N],r[N],w[N];int x,n,k,cas;struct MinCostMaxFlow {int e, ev[M], nxt[M], head[N];typec cost[M], dist[N];typef cap[M];int pnt[N], road[N], q[N], bg, ed;bool vis[N];void init() { e = 0; memset(head,-1,sizeof(head)); }void addedge(int u, int v, typef f, typec c) { //u->v flow=f, cost=cev[e]=v; cap[e]=f; cost[e]=c; nxt[e]=head[u]; head[u]=e++;ev[e]=u; cap[e]=0; cost[e]=-c; nxt[e]=head[v]; head[v]=e++;}bool spfa(int s, int t, int n) {for(int i=0;i<=n;i++) dist[i] = infc, vis[i] = 0;bg = ed = dist[s] = 0;pnt[s] = s; q[ed++] = s;while (bg != ed) {int u = q[bg++]; vis[u] = 0;if (bg == N) bg = 0;for (int i = head[u]; ~i; i = nxt[i]) {if (cap[i] <= 0)continue;int v = ev[i];if (dist[v] > dist[u] + cost[i]) {dist[v] = dist[u] + cost[i];pnt[v] = u; road[v] = i;if (!vis[v]) {q[ed++] = v; vis[v] = 1;if(ed == N)ed = 0;}}}}return dist[t] != infc;}void mincost(int s, int t, int n, typef &f, typec &c) {c = f = 0;while(spfa(s, t, n)){typef minf = inff;for(int u = t; u != s; u = pnt[u])minf = min(minf, cap[road[u]]);for(int u = t; u != s; u = pnt[u]){cap[road[u]] -= minf;cap[road[u] ^ 1] += minf;}f += minf;c += minf * dist[t];}}};int solve(){    int xx,ll,rr;    int ans1,ans;    MinCostMaxFlow tmp;    sort(a,a+x);  xx=0;  b[xx++]=a[0];    for(int i=1;i<x;i++)      if(a[i]!=a[i-1])       b[xx++]=a[i];    tmp.init();    for(int i=1;i<=xx+1;i++)     tmp.addedge(i-1,i,k,0);   for(int i=1;i<=n;i++)    {        for(int j=0;j<xx;j++)          {              if(b[j]==l[i])               ll=j+1;              if(b[j]==r[i])               rr=j+1;          }       tmp.addedge(ll,rr,1,-w[i]);    }    tmp.mincost(0,xx+1,xx+2,ans1,ans);    return -ans;}int main(){    //freopen("F:\\a.txt","r",stdin);    scanf("%d",&cas);    while(cas--)    {        scanf("%d %d",&n,&k);        x=0;        for(int i=1;i<=n;i++)        {            scanf("%d %d %d",&l[i],&r[i],&w[i]);            a[x++]=l[i];  a[x++]=r[i];        }        printf("%d\n",solve());    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.