BZOJ2324[ZJOI2011]营救皮卡丘

来源:互联网 发布:网络对学生的影响 编辑:程序博客网 时间:2024/04/30 07:50

题目

BZOJ2324[ZJOI2011]营救皮卡丘

题解

,有点像限制k条路径的最小路径覆盖啊。
可是最小路径覆盖的模型是个DAG,这是个无向图,我们考虑每个人有用的路径(就是k个人中他第一个到达的点,也就是他摧毁的点),编号肯定递增,如果我们把这k条有用的路径提出来,那就会是一个DAG,DAG边权怎么弄?假设在路径(以下将有用路径称为路径,原图路径称为原路径)中我有这么一个走动(i->j),那么我肯定走i->j的最短路,但是注意这个最短路只能经过编号<=j的点,这个可以用floyd预处理,或者你跑n次Dij也行。然后边就建好了,注意在最小路径覆盖中我们要求每个点只经过一次,这跟题面说的摧毁了节点还能经过并不矛盾,因为我们的最短路允许经过之前的节点。

然后变成了DAG,上最小权路径覆盖,注意源点向起点连边是cap=k,cost=0的,限制有k个人。

代码

//QWsin#include<queue>#include<cstdio>#include<cstring>#include<algorithm>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;}
原创粉丝点击