Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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\n",ax,by); 130 } 131 return 0; 132 }