bzoj2324[ZJOI2011]營救皮卡丘

來源:互聯網
上載者:User

標籤:角度   mem   bzoj   opera   tin   main   name   網路流   namespace   

題面:一張無向帶權連通圖,點數n+1(標號0…n),邊數m,現在有k個人在0號點,要求依次摧毀1,2,3….n號點.假如至少一個人經過了點x,就認為點x被摧毀了.只有編號比x小的點都被摧毀才能經過點x. k個人可以分頭行動.被摧毀的點在摧毀之後可以經過.滿足要求的前提下,求k個人經過的路徑長度之和的最小值.(即每個人走過的路徑長度加起來,一個人走一條路多次時間長度度應多次計算). n<=150,m<=20000,1<=k<=10,邊權<=10000

分析:每個點都必須經過一次,有k個人,這讓我們想到網路流中DAG的最小權路徑覆蓋問題,但這裡的圖是無向圖且有一些經過順序的限制,並且點和邊都能夠多次使用.不過,每個點都只會被摧毀一次,從這個角度入手,我們考慮一個人依次摧毀的所有點,它們一定滿足編號遞增,也就是”一個人摧毀了一個編號較小的點之後,如果沒有停下,一定會摧毀一個編號比它更大的點”,這好像有一些DAG的特徵.仔細觀察,我們發現,如果一個人摧毀點u之後再去摧毀點v,所需經過的最短路徑長度就是從u到v只經過編號小於v的點時的最短路,這個是可以預先處理的.可以floyd,也可以n遍dijkstra.我覺得dijkstra比較好理解,每次跑經過編號小於s的點到達s的最短路,可以直接限制經過的點的編號.於是我們建出一個n+1個點的競賽圖,邊都從編號小的點u指向編號大的點v,邊權為一個人摧毀u後再前往v的最短路(經過編號小於v的點),如果u=0,表示從0前往v的最短路,同樣經過編號小於v的點.

然後拆點,除了0和n,每個點拆成入點和出點(0隻有出點,n只有入點)

對於一條邊(u,v),從u的出點向v的入點連一條費用等於邊權的邊

從源點向1到n-1每個點的出點連一條流量為1費用為0的邊

從源點向0連一條流量為k費用為0的邊(限制k個人)

從每個點的入點向匯點連一條流量為1費用為0的邊,這樣的圖上每個最大流的方案都對應一種可行的方案.跑最小費用最大流即可.

#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;int n,m,k;int d[155][155];namespace Mincost{  const int maxn=500,maxm=300000;  struct edge{    int to,next,w,cost;  }lst[maxm];int len=0,first[maxm];  void addedge(int a,int b,int w,int cost){    lst[len].to=b;lst[len].next=first[a];lst[len].w=w;lst[len].cost=cost;first[a]=len++;    lst[len].to=a;lst[len].next=first[b];lst[len].w=0;lst[len].cost=-cost;first[b]=len++;  }  bool inq[maxn];int q[maxn],dis[maxn],vis[maxn],prt[maxn],head,tail,s,t,T;  bool spfa(){    head=tail=0;q[tail++]=s;dis[s]=0;prt[s]=-1;vis[s]=++T;inq[s]=true;    while(head!=tail){      int x=q[head++];head%=maxn;inq[x]=false;      for(int pt=first[x];pt!=-1;pt=lst[pt].next){    if(lst[pt].w==0)continue;    if(vis[lst[pt].to]!=T||dis[lst[pt].to]>dis[x]+lst[pt].cost){      dis[lst[pt].to]=dis[x]+lst[pt].cost;vis[lst[pt].to]=T;      prt[lst[pt].to]=pt;      if(!inq[lst[pt].to]){        q[tail++]=lst[pt].to;inq[lst[pt].to]=true;tail%=maxn;      }    }      }    }    return vis[t]==T;  }  int mincost(){    int ans=0;    while(spfa()){      ans+=dis[t];      for(int pt=prt[t];pt!=-1;pt=prt[lst[pt^1].to]){    lst[pt].w--;lst[pt^1].w++;      }    }    return ans;  }  void build(){    memset(first,-1,sizeof(first));    s=2*n+1;t=2*n+2;    addedge(s,0,k,0);    for(int i=1;i<=n;++i)addedge(s,i,1,0);    for(int i=1;i<=n;++i)addedge(n+i,t,1,0);    for(int i=1;i<=n;++i)addedge(0,n+i,1,d[0][i]);    for(int i=1;i<=n;++i){      for(int j=i+1;j<=n;++j){    addedge(i,n+j,1,d[i][j]);      }    }  }};namespace Init{  const int maxn=200,maxm=40005;  struct edge{    int to,next,w;  }lst[maxm];int len=1,first[maxn];  void addedge(int a,int b,int w){    lst[len].to=b;lst[len].next=first[a];lst[len].w=w;first[a]=len++;  }  int dis[maxn];bool vis[maxn];  struct node{    int v,d;    node(int _v,int _d){      v=_v;d=_d;    }    bool operator <(const node &B)const{      return d>B.d;    }  };  void dijkstra(int s){    memset(vis,0,sizeof(vis));    priority_queue<node> q;    q.push(node(s,0));    while(!q.empty()){      node tmp=q.top();q.pop();      if(vis[tmp.v])continue;      vis[tmp.v]=true;dis[tmp.v]=tmp.d;      for(int pt=first[tmp.v];pt;pt=lst[pt].next){    if(lst[pt].to<s&&!vis[lst[pt].to])q.push(node(lst[pt].to,lst[pt].w+tmp.d));      }    }    for(int i=0;i<=s;++i)d[i][s]=dis[i];  }  void init(){    scanf("%d%d%d",&n,&m,&k);    int a,b,w;    for(int i=1;i<=m;++i){      scanf("%d%d%d",&a,&b,&w);addedge(a,b,w);addedge(b,a,w);    }    memset(d,0x3f,sizeof(d));    for(int i=1;i<=n;++i)dijkstra(i);  }  };int main(){  Init::init();  Mincost::build();  printf("%d\n",Mincost::mincost());  return 0;}

 

bzoj2324[ZJOI2011]營救皮卡丘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.