• hdu 4411 Arrest


    Arrest

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 4411
    64-bit integer IO format: %I64d      Java class name: Main
     
    There are (N+1) cities on TAT island. City 0 is where police headquarter located. The economy of other cities numbered from 1 to N ruined these years because they are all controlled by mafia. The police plan to catch all the mafia gangs in these N cities all over the year, and they want to succeed in a single mission. They figure out that every city except city 0 lives a mafia gang, and these gangs have a simple urgent message network: if the gang in city i (i>1) is captured, it will send an urgent message to the gang in city i-1 and the gang in city i -1 will get the message immediately. 
    The mission must be carried out very carefully. Once a gang received an urgent message, the mission will be claimed failed.
    You are given the map of TAT island which is an undirected graph. The node on the graph represents a city, and the weighted edge represents a road between two cities(the weight means the length). Police headquarter has sent k squads to arrest all the mafia gangs in the rest N cities. When a squad passes a city, it can choose to arrest the gang in the city or to do nothing. These squads should return to city 0 after the arrest mission.
    You should ensure the mission to be successful, and then minimize the total length of these squads traveled.
     

    Input

    There are multiple test cases.
    Each test case begins with a line with three integers N, M and k, here M is the number of roads among N+1 cities. Then, there are M lines. Each of these lines contains three integers X, Y, Len, which represents a Len kilometer road between city X and city Y. Those cities including city 0 are connected. 
    The input is ended by “0 0 0”.
    Restrictions: 1 ≤ N ≤ 100, 1 ≤ M ≤ 4000, 1 ≤ k ≤ 25, 0 ≤ Len ≤ 1000
     

    Output

    For each test case,output a single line with a single integer that represents the minimum total length of these squads traveled.
     

    Sample Input

    3 4 2
    0 1 3
    0 2 4
    1 3 2
    2 3 2
    0 0 0

    Sample Output

    14

    Source

     
    解题:
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <climits>
      7 #include <vector>
      8 #include <queue>
      9 #include <cstdlib>
     10 #include <string>
     11 #include <set>
     12 #include <stack>
     13 #define LL long long
     14 #define pii pair<int,int>
     15 using namespace std;
     16 const int maxn = 600;
     17 const int INF = 0x3f3f3f;
     18 struct arc{
     19     int to,flow,cost,next;
     20     arc(int x = 0,int y = 0,int z = 0,int nxt = -1){
     21         to = x;
     22         flow = y;
     23         cost = z;
     24         next = nxt;
     25     }
     26 };
     27 arc e[maxn*maxn];
     28 int head[maxn],d[maxn],p[maxn],dis[maxn][maxn];
     29 int tot,S,T,N,M,K;
     30 void add(int u,int v,int flow,int cost){
     31     e[tot] = arc(v,flow,cost,head[u]);
     32     head[u] = tot++;
     33     e[tot] = arc(u,0,-cost,head[v]);
     34     head[v] = tot++;
     35 }
     36 bool in[maxn];
     37 bool spfa(){
     38     queue<int>q;
     39     const int inf = 0x3f3f3f3f;
     40     for(int i = 0; i < maxn; ++i){
     41         p[i] = -1;
     42         in[i] = false;
     43         d[i] = inf;
     44     }
     45     d[S] = 0;
     46     q.push(S);
     47     while(!q.empty()){
     48         int u = q.front();
     49         q.pop();
     50         in[u] = false;
     51         for(int i = head[u]; ~i; i = e[i].next){
     52             if(e[i].flow && d[e[i].to] > d[u] + e[i].cost){
     53                 d[e[i].to] = d[u] + e[i].cost;
     54                 p[e[i].to] = i;
     55                 if(!in[e[i].to]){
     56                     in[e[i].to] = true;
     57                     q.push(e[i].to);
     58                 }
     59             }
     60         }
     61     }
     62     return p[T] > -1;
     63 }
     64 int solve(){
     65     int ans = 0;
     66     while(spfa()){
     67         int minF = INF;
     68         for(int i = p[T]; ~i; i = p[e[i^1].to])
     69             minF = min(minF,e[i].flow);
     70         for(int i = p[T]; ~i; i = p[e[i^1].to]){
     71             e[i].flow -= minF;
     72             e[i^1].flow += minF;
     73         }
     74         ans += d[T]*minF;
     75     }
     76     return ans;
     77 }
     78 void Floyd(){
     79         for(int k = 0; k <= N; ++k){
     80             for(int i = 0; i <= N; ++i)
     81                 for(int j = 0; j <= N; ++j)
     82                     dis[i][j] = min(dis[i][j],dis[i][k]+dis[k][j]);
     83         }
     84 }
     85 int main(){
     86     int u,v,w;
     87     while(scanf("%d %d %d",&N,&M,&K),N||M||K){
     88         memset(head,-1,sizeof(head));
     89         for(int i = tot = 0; i <= N; ++i){
     90             for(int j = 0; j <= N; ++j)
     91                 dis[i][j] = i == j?0:INF;
     92         }
     93         for(int i = 0; i < M; ++i){
     94             cin>>u>>v>>w;
     95             dis[u][v] = dis[v][u] = min(dis[u][v],w);
     96         }
     97         S = 2*N + 1;
     98         T = S + 1;
     99         Floyd();
    100         add(S,0,K,0);
    101         add(0,T,K,0);
    102         for(int i = 1; i <= N; ++i){
    103             add(0,i,1,dis[0][i]);
    104             add(i+N,T,1,dis[0][i]);
    105             add(i,i+N,1,-INF);
    106             for(int j = i + 1; j <= N; ++j)
    107                 add(i + N,j,1,dis[i][j]);
    108         }
    109         cout<<solve() + N*INF<<endl;
    110     }
    111     return 0;
    112 }
    View Code
  • 相关阅读:
    软件项目功能测试框架(转载自51Testing软件测试)
    【性能测试】服务器资源监测工具sar安装
    Fiddler对https抓包时,提示"HTTPS decryption is disabled."原因及破解
    Postgresql空库发布或者部分空库,模式,表处理备份流程
    PostgreSQL完整备份与还原过程
    Excel:一列是源值随机加减某随机值,变为另一列的数值
    Postgresql个人维护库时,出现有用户在连接又找不到这个用户是谁的强制中断连接的方法;
    切换或者用户登录时 出现 显示 -bash-4.2$ 问题 的解决
    postgresql 9.4.4 源码安装
    搜索项中,文案修改导致搜索无法使用
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4146827.html
Copyright © 2020-2023  润新知