• hdu-5889-最短路+网络流/最小割


    Barricade

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 2098    Accepted Submission(s): 616


    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
     
        给出一个无向图,每条边的长度都是1,第i条边建立障碍费用为wi,在保证从1到N号点的所有
    的最短路径上都有障碍的情况下使得花费最小,输出这个费用。
      将所有非最短路上的边都去除之后,问题转化为求当前图的最小割(因为要所有S-T路径上都至
    少出现一条边是是障碍边,也就是说将障碍边去除之后S-T不在联通),跑一下最大流就好了。
     
      
      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 #define inf 0x3f3f3f3f
      4 #define pii pair<int,int>
      5 #define mp make_pair
      6 struct Edge
      7 {
      8   int v,cap,flow,next;
      9 }e[200020];
     10 vector<int> g[1010];
     11 int first[1010],d[1010],cur[1010],tot,N;
     12 bool vis[1010];
     13 void add(int u,int v,int cap){
     14   e[tot]=Edge{v,cap,0,first[u]};
     15   first[u]=tot++;
     16 }
     17 int dij(){
     18     memset(d,inf,sizeof(d));
     19     memset(vis,0,sizeof(vis));
     20     d[1]=0;
     21     priority_queue<pii,vector<pii>,greater<pii> >q;
     22     q.push(mp(0,1));
     23     while(!q.empty()){
     24       int u=q.top().second;q.pop();
     25       if(vis[u]) continue;
     26       vis[u]=1;
     27       for(int i=0;i<g[u].size();++i){
     28         if(d[g[u][i]]>d[u]+1){
     29           d[g[u][i]]=d[u]+1;
     30           q.push(mp(d[g[u][i]],g[u][i]));
     31         }
     32       }
     33     }
     34     return d[N];
     35 }
     36 bool bfs(){
     37   memset(d,0,sizeof(d));
     38   memset(vis,0,sizeof(vis));
     39   queue<int>q;
     40   q.push(1);
     41   d[1]=0;
     42   vis[1]=1;
     43   while(!q.empty()){
     44     int u=q.front();q.pop();
     45       for(int i=first[u];~i;i=e[i].next){
     46         if(!vis[e[i].v] && e[i].cap-e[i].flow>0){
     47           vis[e[i].v]=1;
     48           d[e[i].v]=d[u]+1;
     49           q.push(e[i].v);
     50         }
     51       }
     52   }
     53   return vis[N];
     54 }
     55 int dfs(int u,int a){
     56   if(u==N || a==0) return a;
     57   int f,ans=0;
     58   for(int &i=cur[u];~i;i=e[i].next){
     59     if(d[e[i].v]==d[u]+1 && (f=dfs(e[i].v,min(a,e[i].cap-e[i].flow)))>0){
     60       e[i].flow+=f;
     61       e[i^1].flow-=f;
     62       a-=f;
     63       ans+=f;
     64       if(!a) break;
     65     }
     66   }
     67   return ans;
     68 }
     69 void solve(){
     70   int ans=0;
     71   while(bfs()){
     72     for(int i=1;i<=N;++i) cur[i]=first[i];
     73     ans+=dfs(1,inf);
     74   }
     75   printf("%d
    ",ans);
     76 }
     77 int main(){
     78   int M,t,i,j,k;
     79   int u[10100],v[10100],w[10100];
     80   cin>>t;
     81   while(t--){
     82     scanf("%d%d",&N,&M);
     83     memset(first,-1,sizeof(first));
     84     tot=0;
     85     for(i=1;i<=N;++i) g[i].clear();
     86     for(i=1;i<=M;++i){
     87       scanf("%d%d%d",u+i,v+i,w+i);
     88       g[u[i]].push_back(v[i]);
     89       g[v[i]].push_back(u[i]);
     90     }
     91     dij();
     92     for(i=1;i<=M;++i){
     93       if(d[u[i]]+1==d[v[i]]){
     94         add(u[i],v[i],w[i]),add(v[i],u[i],0);
     95 
     96       }
     97       if(d[v[i]]+1==d[u[i]]){
     98         add(v[i],u[i],w[i]),add(u[i],v[i],0);
     99       }
    100     }
    101     solve();
    102   }
    103   return 0;
    104 }
  • 相关阅读:
    python day2 省市三级联动
    Dynamics CRM绑定表单查看当前表单的数据参数传递
    Dynamics CRM制作报表的时候让用户可以用自己的权限浏览数据
    Sql Server Report Service访问服务页面503解决方法
    Dynamics CRM报表无法访问提示“报表服务器无法访问或使用加密密钥。你可能需要将服务器添加到扩展组,或重新导入”
    Dynamics CRM报表点击自动运行方法
    C#委托的学习了解
    Google不兼容ShowModalDialog()弹出对话框的解决办法
    Dynamics CRM报表提示rsProcessingAborted解决方法
    Dynamics CRM邮箱配置
  • 原文地址:https://www.cnblogs.com/zzqc/p/9398606.html
Copyright © 2020-2023  润新知