hdu5889最短路+网络流

    xiaoxiao2023-02-02  13

    Barricade

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 57 Problem Description The empire is under attack again. The general of empire is planning to defend his castle. The land can be seen as  N  towns and  M  roads, and each road has the same length and connects two towns. The town numbered  1  is where general's castle is located, and the town numbered  N  is where the enemies are staying. The general supposes that the enemies would choose a shortest path. He knows his army is not ready to fight and he needs more time. Consequently he decides to put some barricades on some roads to slow down his enemies. Now, he asks you to find a way to set these barricades to make sure the enemies would meet at least one of them. Moreover, the barricade on the  i -th road requires  wi  units of wood. Because of lacking resources, you need to use as less wood as possible.   Input The first line of input contains an integer  t , then  t  test cases follow. For each test case, in the first line there are two integers  N(N1000)  and  M(M10000) . The  i -the line of the next  M  lines describes the  i -th edge with three integers  u,v  and  w  where  0w1000  denoting an edge between  u  and  v  of barricade cost  w .   Output For each test cases, output the minimum wood cost.   Sample Input 1 4 4 1 2 1 2 4 2 3 1 3 4 3 4   Sample Output 4   Source 2016 ACM/ICPC Asia Regional Qingdao Online      青岛网络赛,题意:N个点M 条路径,每条路径长度为1,敌人从M节点点要进攻1节点,敌人总是选择最优路径即最短路径来进攻我方,为了阻止敌人,我们要把一些路封死,每条路径封死需要一些花费,求最小花费;      先建一个路径长度为都为1的图,跑两 遍最短路,分别是从1点出发的最短路和从N点出发的最短路,然后遍历所有边,如果该边是最短路上的路径着将该条边加入新图,意味着新图是所有的最短路径组成的图,流量为花费,然后求一遍最大流,得到最小割,最小割即为最小花费, AC代码: #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> using namespace std; const int maxn=1e6+100,inf=0x3f3f3f3f; const int MAXN=1e6+100,MAXM=1e6+100; int d1[maxn],d2[maxn],d[maxn],vis1[maxn]; struct node1 { int v,next,cost,cap; }e1[maxn]; int tot1,head1[maxn]; void init() { tot1=0; memset(head1,-1,sizeof(head1)); } void add_edge(int u,int v,int cost,int cap) { e1[tot1].cap=cap; e1[tot1].cost=cost; e1[tot1].v=v; e1[tot1].next=head1[u]; head1[u]=tot1++; } bool spfa(int start,int n) { memset(vis1,0,sizeof(vis1)); for(int i=1;i<=n;i++) d[i]=inf; vis1[start]=1; d[start]=0; queue<int>que; while(!que.empty()) que.pop(); que.push(start); while(!que.empty()) { int u=que.front(); que.pop(); vis1[u]=0; for(int i=head1[u];i!=-1;i=e1[i].next) { int v=e1[i].v; if(d[v]>d[u]+e1[i].cost) { d[v]=d[u]+e1[i].cost; if(!vis1[v]) { vis1[v]=1; que.push(v); } } } } } struct Node { int to,next,cap; }edge[MAXM]; int tol;int head[MAXN]; int gap[MAXN],dis[MAXN],pre[MAXN],cur[MAXN]; void init2() { tol=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int w,int rw=0) { edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++; edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];head[v]=tol++; } int sap(int start,int end,int nodenum) { memset(dis,0,sizeof(dis)); memset(gap,0,sizeof(gap)); memcpy(cur,head,sizeof(head)); int u=pre[start]=start,maxflow=0,aug=-1; gap[0]=nodenum; while(dis[start]<nodenum) { loop: for(int &i=cur[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap&&dis[u]==dis[v]+1) { if(aug==-1||aug>edge[i].cap) aug=edge[i].cap; pre[v]=u; u=v; if(v==end) { maxflow+=aug; for(u=pre[u];v!=start;v=u,u=pre[u]) { edge[cur[u]].cap-=aug; edge[cur[u]^1].cap+=aug; } aug=-1; } goto loop; } } int mindis=nodenum; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(edge[i].cap&&mindis>dis[v]) { cur[u]=i; mindis=dis[v]; } } if((--gap[dis[u]])==0)break; gap[dis[u]=mindis+1]++; u=pre[u]; } return maxflow; } int vist[maxn]; int main() { int t,n,m; int u,v,c; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); init(); for(int i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&c); { add_edge(u,v,1,c); add_edge(v,u,1,c); } } spfa(1,n); memcpy(d1,d,sizeof(d)); spfa(n,n); init2(); memset(vist,0,sizeof(vist)); for( u=1;u<=n;u++) { for(int i=head1[u];i!=-1;i=e1[i].next) { v=e1[i].v; // cout<<d1[u]<<" "<<d[v]<<" "<<e1[i].cost<<endl; if(d1[u]+d[v]+e1[i].cost==d1[n]) { // cout<<"gg"<<endl; addedge(u,v,e1[i].cap); vist[u]=1; vist[v]=1; } } } int num=0; for(int i=1;i<=n;i++) { if(vist[i]) num++; } int ans=sap(1,n,num); printf("%d\n",ans); } return 0; } /** 5 5 1 2 1 2 3 1 3 5 1 1 4 2 4 5 3*/  
    转载请注明原文地址: https://ju.6miu.com/read-1138590.html
    最新回复(0)