zoukankan      html  css  js  c++  java
  • 2015 Multi-University Training Contest 1 Tricks Device

    Tricks Device

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 0    Accepted Submission(s): 0


    Problem Description
    Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the entrance of the tomb while Dumb Zhang’s at the end of it. The tomb is made up of many chambers, the total number is N. And there are M channels connecting the chambers. Innocent Wu wants to catch up Dumb Zhang to find out the answers of some questions, however, it’s Dumb Zhang’s intention to keep Innocent Wu in the dark, to do which he has to stop Innocent Wu from getting him. Only via the original shortest ways from the entrance to the end of the tomb costs the minimum time, and that’s the only chance Innocent Wu can catch Dumb Zhang.
    Unfortunately, Dumb Zhang masters the art of becoming invisible(奇门遁甲) and tricks devices of this tomb, he can cut off the connections between chambers by using them. Dumb Zhang wanders how many channels at least he has to cut to stop Innocent Wu. And Innocent Wu wants to know after how many channels at most Dumb Zhang cut off Innocent Wu still has the chance to catch Dumb Zhang.
     
    Input
    There are multiple test cases. Please process till EOF.
    For each case,the first line must includes two integers, N(<=2000), M(<=60000). N is the total number of the chambers, M is the total number of the channels.
    In the following M lines, every line must includes three numbers, and use ai、bi、li as channel i connecting chamber ai and bi(1<=ai,bi<=n), it costs li(0<li<=100) minute to pass channel i.
    The entrance of the tomb is at the chamber one, the end of tomb is at the chamber N.
     
    Output
    Output two numbers to stand for the answers of Dumb Zhang and Innocent Wu’s questions.
     
    Sample Input
    8 9
    1 2 2
    2 3 2
    2 4 1
    3 5 3
    4 5 4
    5 8 1
    1 6 2
    6 7 5
    7 8 1
     
    Sample Output
    2 6
     
    解题:最短路+最小割
     
    先把所有的最短路提取到另一份图中,然后看看最少经过几条边(可以用dp优化,或者标记已经访问的边来加速)可以由终点到起点
    m-减去最少的可经过的边 即可删除的边
     
    然后再对刚才提取的图 求最小割,最小割即为最少删除几条边,可以使得最短路变长
     
    需要注意重边的影响
     
     
      1 #include <bits/stdc++.h>
      2 #define pii pair<int,int>
      3 using namespace std;
      4 const int maxn = 1000010;
      5 const int INF = 0x3f3f3f3f;
      6 struct arc {
      7     int to,w,next,id;
      8     arc(int x = 0,int y = 0,int z = -1) {
      9         to = x;
     10         w = y;
     11         next = z;
     12     }
     13 } e[maxn];
     14 int d[maxn],tot,S,T,head[maxn],cur[maxn];
     15 vector< pii >g[maxn];
     16 void add(int u,int v,int wa,int wb,int id = 0) {
     17     e[tot] = arc(v,wa,head[u]);
     18     e[tot].id = id;
     19     head[u] = tot++;
     20     e[tot] = arc(u,wb,head[v]);
     21     e[tot].id = id;
     22     head[v] = tot++;
     23 }
     24 bool done[maxn];
     25 priority_queue< pii,vector< pii >,greater< pii > >q;
     26 void dijkstra() {
     27     while(!q.empty()) q.pop();
     28     memset(d,0x3f,sizeof d);
     29     d[S] = 0;
     30     memset(done,false,sizeof done);
     31     q.push(pii(d[S],S));
     32     while(!q.empty()) {
     33         int u = q.top().second;
     34         q.pop();
     35         if(done[u]) continue;
     36         done[u] = true;
     37         for(int i = head[u]; ~i; i = e[i].next) {
     38             if(d[e[i].to] > d[u] + e[i].w) {
     39                 d[e[i].to] = d[u] + e[i].w;
     40                 g[e[i].to].clear();
     41                 g[e[i].to].push_back(pii(u,e[i].id));
     42                 q.push(pii(d[e[i].to],e[i].to));
     43             } else if(d[e[i].to] == d[u]+e[i].w) {
     44                 g[e[i].to].push_back(pii(u,e[i].id));
     45                 q.push(pii(d[e[i].to],e[i].to));
     46             }
     47         }
     48     }
     49 }
     50 int minstep;
     51 void dfs(int u,int dep,int fa) {
     52     if(u == S) {
     53         minstep = min(dep,minstep);
     54         return;
     55     }
     56     for(int i = g[u].size()-1; i >= 0; --i) {
     57         if(g[u][i].first == fa) continue;
     58         dfs(g[u][i].first,dep+1,u);
     59         bool flag = true;
     60         for(int j = head[g[u][i].first]; flag && ~j; j = e[j].next) {
     61             if(e[j].id == g[u][i].second) flag = false;
     62         }
     63        if(flag) {
     64             add(g[u][i].first,u,1,0,g[u][i].second);
     65             //cout<<g[u][i]<<" *** "<<u<<endl;
     66        }
     67     }
     68 }
     69 bool bfs() {
     70     queue<int>q;
     71     memset(d,-1,sizeof d);
     72     d[S] = 1;
     73     q.push(S);
     74     while(!q.empty()) {
     75         int u = q.front();
     76         q.pop();
     77         for(int i = head[u]; ~i; i = e[i].next) {
     78             if(e[i].w && d[e[i].to] == -1) {
     79                 d[e[i].to] = d[u] + 1;
     80                 q.push(e[i].to);
     81             }
     82         }
     83     }
     84     return d[T] > -1;
     85 }
     86 int dfs(int u,int low) {
     87     if(u == T) return low;
     88     int tmp = 0,a;
     89     for(int &i = cur[u]; ~i; i = e[i].next) {
     90         if(e[i].w &&d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,min(e[i].w,low)))) {
     91             e[i].w -= a;
     92             e[i^1].w += a;
     93             low -= a;
     94             tmp += a;
     95             if(!low) break;
     96         }
     97     }
     98     if(!tmp) d[u] = -1;
     99     return tmp;
    100 }
    101 int dinic() {
    102     int ret = 0;
    103     while(bfs()) {
    104         memcpy(cur,head,sizeof head);
    105         ret += dfs(S,INF);
    106     }
    107     return ret;
    108 }
    109 int main() {
    110     int n,m,u,v,w;
    111     while(~scanf("%d%d",&n,&m)) {
    112         for(int i = tot = 0; i < maxn; ++i) {
    113             g[i].clear();
    114             head[i] = -1;
    115         }
    116         for(int i = 0; i < m; ++i) {
    117             scanf("%d%d%d",&u,&v,&w);
    118             add(u,v,w,w,i);
    119         }
    120         S = 1;
    121         T = n;
    122         dijkstra();
    123         minstep = INT_MAX;
    124         memset(head,-1,sizeof head);
    125         tot = 0;
    126         dfs(T,0,-1);
    127         int by = m-minstep;
    128         int ax = dinic();
    129         printf("%d %d
    ",ax,by);
    130     }
    131     return 0;
    132 }
    View Code

    重新写了下,思路更清楚些

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 using PII = pair<int,int>;
      4 const int maxn = 2010;
      5 const int INF = 0x3f3f3f3f;
      6 struct arc {
      7     int to,w,next;
      8     arc(int x = 0,int y = 0,int z = -1) {
      9         to = x;
     10         w = y;
     11         next = z;
     12     }
     13 } e[1000100];
     14 int head[maxn],hd[maxn],tot,S,T,n,m;
     15 int gap[maxn],d[maxn];
     16 bool done[maxn];
     17 void add(int head[maxn],int u,int v,int wa,int wb) {
     18     e[tot] = arc(v,wa,head[u]);
     19     head[u] = tot++;
     20     e[tot] = arc(u,wb,head[v]);
     21     head[v] = tot++;
     22 }
     23 void dijkstra() {
     24     memset(d,0x3f,sizeof d);
     25     memset(done,false,sizeof done);
     26     priority_queue<PII,vector<PII>,greater<PII>>q;
     27     d[S] = 0;
     28     q.push(PII(0,S));
     29     while(!q.empty()) {
     30         int u = q.top().second;
     31         q.pop();
     32         if(done[u]) continue;
     33         done[u] = true;
     34         for(int i = hd[u]; ~i; i = e[i].next) {
     35             if(d[e[i].to] > d[u] + e[i].w) {
     36                 d[e[i].to] = d[u] + e[i].w;
     37                 q.push(PII(d[e[i].to],e[i].to));
     38             }
     39         }
     40     }
     41 }
     42 void build() {
     43     for(int i = 1; i <= n; ++i) {
     44         for(int j = hd[i]; ~j; j = e[j].next) {
     45             if(d[e[j].to] == d[i] + e[j].w)
     46                 add(head,i,e[j].to,1,0);
     47         }
     48     }
     49 }
     50 int bfs() {
     51     memset(gap,0,sizeof gap);
     52     memset(d,-1,sizeof d);
     53     queue<int>q;
     54     d[T] = 0;
     55     q.push(T);
     56     while(!q.empty()) {
     57         int u = q.front();
     58         q.pop();
     59         ++gap[d[u]];
     60         for(int i = head[u]; ~i; i = e[i].next) {
     61             if(d[e[i].to] == -1) {
     62                 d[e[i].to] = d[u] + 1;
     63                 q.push(e[i].to);
     64             }
     65         }
     66     }
     67     return d[S];
     68 }
     69 int dfs(int u,int low) {
     70     if(u == T) return low;
     71     int tmp = 0,minH = n - 1;
     72     for(int i = head[u]; ~i; i = e[i].next) {
     73         if(e[i].w) {
     74             if(d[e[i].to] + 1 == d[u]) {
     75                 int a = dfs(e[i].to,min(e[i].w,low));
     76                 e[i].w -= a;
     77                 e[i^1].w += a;
     78                 tmp += a;
     79                 low -= a;
     80                 if(!low) break;
     81                 if(d[S] >= n) return tmp;
     82             }
     83             if(e[i].w) minH = min(minH,d[e[i].to]);
     84         }
     85     }
     86     if(!tmp) {
     87         if(--gap[d[u]] == 0) d[S] = n;
     88         ++gap[d[u] = minH + 1];
     89     }
     90     return tmp;
     91 }
     92 int sap(int ret = 0) {
     93     while(d[S] < n) ret += dfs(S,INF);
     94     return ret;
     95 }
     96 int main() {
     97     int u,v,w;
     98     while(~scanf("%d%d",&n,&m)) {
     99         memset(head,-1,sizeof head);
    100         memset(hd,-1,sizeof hd);
    101         for(int i = tot = 0; i < m; ++i) {
    102             scanf("%d%d%d",&u,&v,&w);
    103             add(hd,u,v,w,w);
    104         }
    105         S = 1;
    106         T = n;
    107         dijkstra();
    108         build();
    109         int y = m - bfs(),x = sap();
    110         printf("%d %d
    ",x,y);
    111     }
    112     return 0;
    113 }
    View Code

     SPFA貌似更快些,这图稀疏

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 using PII = pair<int,int>;
      4 const int maxn = 2010;
      5 const int INF = 0x3f3f3f3f;
      6 struct arc {
      7     int to,w,next;
      8     arc(int x = 0,int y = 0,int z = -1) {
      9         to = x;
     10         w = y;
     11         next = z;
     12     }
     13 } e[1000100];
     14 int head[maxn],hd[maxn],tot,S,T,n,m;
     15 int gap[maxn],d[maxn];
     16 bool in[maxn] = {};
     17 void add(int head[maxn],int u,int v,int wa,int wb) {
     18     e[tot] = arc(v,wa,head[u]);
     19     head[u] = tot++;
     20     e[tot] = arc(u,wb,head[v]);
     21     head[v] = tot++;
     22 }
     23 void dijkstra() {
     24     memset(d,0x3f,sizeof d);
     25     queue<int>q;
     26     d[S] = 0;
     27     q.push(S);
     28     while(!q.empty()){
     29         int u = q.front();
     30         q.pop();
     31         in[u] = false;
     32         for(int i = hd[u]; ~i; i = e[i].next){
     33             if(d[e[i].to] > d[u] + e[i].w){
     34                 d[e[i].to] = d[u] + e[i].w;
     35                 if(!in[e[i].to]){
     36                     in[e[i].to] = true;
     37                     q.push(e[i].to);
     38                 }
     39             }
     40         }
     41     }
     42 }
     43 void build() {
     44     for(int i = 1; i <= n; ++i) {
     45         for(int j = hd[i]; ~j; j = e[j].next) {
     46             if(d[e[j].to] == d[i] + e[j].w)
     47                 add(head,i,e[j].to,1,0);
     48         }
     49     }
     50 }
     51 int bfs() {
     52     memset(gap,0,sizeof gap);
     53     memset(d,-1,sizeof d);
     54     queue<int>q;
     55     d[T] = 0;
     56     q.push(T);
     57     while(!q.empty()) {
     58         int u = q.front();
     59         q.pop();
     60         ++gap[d[u]];
     61         for(int i = head[u]; ~i; i = e[i].next) {
     62             if(d[e[i].to] == -1) {
     63                 d[e[i].to] = d[u] + 1;
     64                 q.push(e[i].to);
     65             }
     66         }
     67     }
     68     return d[S];
     69 }
     70 int dfs(int u,int low) {
     71     if(u == T) return low;
     72     int tmp = 0,minH = n - 1;
     73     for(int i = head[u]; ~i; i = e[i].next) {
     74         if(e[i].w) {
     75             if(d[e[i].to] + 1 == d[u]) {
     76                 int a = dfs(e[i].to,min(e[i].w,low));
     77                 e[i].w -= a;
     78                 e[i^1].w += a;
     79                 tmp += a;
     80                 low -= a;
     81                 if(!low) break;
     82                 if(d[S] >= n) return tmp;
     83             }
     84             if(e[i].w) minH = min(minH,d[e[i].to]);
     85         }
     86     }
     87     if(!tmp) {
     88         if(--gap[d[u]] == 0) d[S] = n;
     89         ++gap[d[u] = minH + 1];
     90     }
     91     return tmp;
     92 }
     93 int sap(int ret = 0) {
     94     while(d[S] < n) ret += dfs(S,INF);
     95     return ret;
     96 }
     97 int main() {
     98     int u,v,w;
     99     while(~scanf("%d%d",&n,&m)) {
    100         memset(head,-1,sizeof head);
    101         memset(hd,-1,sizeof hd);
    102         for(int i = tot = 0; i < m; ++i) {
    103             scanf("%d%d%d",&u,&v,&w);
    104             add(hd,u,v,w,w);
    105         }
    106         S = 1;
    107         T = n;
    108         dijkstra();
    109         build();
    110         int y = m - bfs(),x = sap();
    111         printf("%d %d
    ",x,y);
    112     }
    113     return 0;
    114 }
    View Code
  • 相关阅读:
    MB52增强
    采购订单创建、修改、审批增强ME21N/ME22N/ME28/ME29N
    SAP调用RestfulApi接口POST数据到外部系统
    SAP ABAP: 把内表数据以excel或csv格式,通过前台或者后台的方式上传至FTP服务器
    Docker 入门
    CentOS 扩容
    ubuntu查看防火墙状态
    No module named ds_store
    Django Ajax 实现历史图形查询
    Django 前端BootCSS 实现分页
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4665345.html
Copyright © 2011-2022 走看看