Frequency Hopping
64-bit integer IO format: %lld Java class name: Main
20th July, 1942
Colonel Al Pacheno,According to the previous order “ref: 232/UK44i/334sda#nh$X3y”, you are required back in the DOI (Department of intelligence, London) to head the special programming contingent immediately. You are to assign a programmer for the job whose specification is attached with this letter. Level 3 Secrecy must be maintained.
Sincerely,
General Shaan Konary
Director, DOI
London
Ps: Sorry to ruin your Caribbean holiday
232/UK44i/334sda#nh$X3y/Appx-301a
At this moment, through out Europe, our base station numbers 1 to N are actively operational
through wireless channels. Immediately we require sending C secret message fragments from
our head quarters (base station 1) to Nth base station. Germans have developed Zämmhäim – a
machine which jams the frequency channel between base stations after a station has sent a
message fragment. In that case, the base stations must transmit using a different frequency
channel for each message fragment. There are several unidirectional channels set up between
base stations at this moment. We can only make arrangements to set up number of frequency
channels only between two base stations. Your task is to check whether all the message
fragments can be sent to the desired base station with or without increasing frequency channel
between any two particular base stations. You have to give us all possible options if it is
required to increase frequency channel between two stations.
--End of Attachment
As members of Secret Programmers Group (SPG) you are assigned to solve this problem within 5 hrs
and deliver the solution directly to Colonel Al Pacheno. You have to maintain Level 3 secrecy and
destroy all documents corresponding to this as soon as you deliver the solution.
Input:
There will be multiple test cases. The first line of each test case contains three numbers N, E and C
where N (0<N<101) represents the number of base stations, E (E<10000) represents the number of
available connections between the base stations and C (C<2000000000) represents the number of
secret message fragments that are required to send from station 1 to station N. After that, there will be
E lines. Each line contains 3 numbers: b1(0<b1<101), b2(0<b2<101) and fp (0<fp<5001) which
represent the number of frequency channels available currently from b1 to b2. Input is terminated when
N=E=C=0. 1
Output:
For each test case, there will be one line of output. First, you have to print the case number. If it is
possible to send C secret message fragments from the current status the output will be “possible”.
Otherwise, you have to print all pairs of stations (in ascending order) if it is possible send the required
message fragments by increasing the frequency channel between any one of them. If it is still
impossible, you have to print “not possible”.
Sample Input Output for Sample Input
4 4 5
1 2 5
1 3 5
2 4 5
3 4 5
4 4 5
1 2 1
1 3 5
2 4 5
3 4 1
4 4 5
1 2 1
1 3 1
2 4 1
3 4 1
0 0 0
Case 1: possible
Case 2: possible option:(1,2),(3,4)
Case 3: not possible
Problemsetter: Syed Monowar Hossain
Special Thanks: Abdullah al Mahmud
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 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 20000; 18 struct arc { 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = -1) { 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 arc e[maxn*20]; 27 int head[maxn],d[maxn],cur[maxn]; 28 int tot,S,T,N,E,C,cnt; 29 void add(int u,int v,int flow) { 30 e[tot] = arc(v,flow,head[u]); 31 head[u] = tot++; 32 e[tot] = arc(u,0,head[v]); 33 head[v] = tot++; 34 } 35 bool bfs() { 36 memset(d,-1,sizeof(d)); 37 queue<int>q; 38 d[S] = 1; 39 q.push(S); 40 while(!q.empty()) { 41 int u = q.front(); 42 q.pop(); 43 for(int i = head[u]; ~i; i = e[i].next) { 44 if(e[i].flow > 0 && d[e[i].to] == -1) { 45 d[e[i].to] = d[u] + 1; 46 q.push(e[i].to); 47 } 48 } 49 } 50 return d[T] > -1; 51 } 52 pii rec[maxn*20]; 53 int dfs(int u,int low) { 54 if(u == T) return low; 55 int tmp = 0,a; 56 for(int &i = cur[u]; ~i; i = e[i].next) { 57 if(e[i].flow > 0&& d[e[i].to] == d[u]+1&&(a=dfs(e[i].to,min(low,e[i].flow)))) { 58 e[i].flow -= a; 59 e[i^1].flow += a; 60 low -= a; 61 tmp += a; 62 rec[cnt++] = make_pair(i,a); 63 rec[cnt++] = make_pair(i^1,-a); 64 if(!low) break; 65 } 66 } 67 if(!tmp) d[u] = -1; 68 return tmp; 69 } 70 int dinic() { 71 int ans = 0; 72 cnt = 0; 73 while(bfs()) { 74 memcpy(cur,head,sizeof(head)); 75 ans += dfs(S,INF); 76 } 77 return ans; 78 } 79 void release(){ 80 for(int i = 0; i < cnt; ++i) 81 e[rec[i].first].flow += rec[i].second; 82 } 83 vector< pii >ans; 84 int main() { 85 int u,v,w,cs = 1; 86 while(scanf("%d %d %d",&N,&E,&C),N||E||C) { 87 memset(head,-1,sizeof(head)); 88 S = 1; 89 T = N; 90 for(int i = 0; i < E; ++i) { 91 scanf("%d %d %d",&u,&v,&w); 92 add(u,v,w); 93 } 94 int flow = dinic(); 95 printf("Case %d: ",cs++); 96 if(flow >= C) puts("possible"); 97 else { 98 ans.clear(); 99 for(int i = 0; i < tot; i += 2) { 100 if(e[i].flow == 0){ 101 e[i].flow = C; 102 if(flow + dinic() >= C) ans.push_back(make_pair(e[i^1].to,e[i].to)); 103 release(); 104 e[i].flow = 0; 105 } 106 } 107 if(ans.size()){ 108 printf("possible option:"); 109 sort(ans.begin(),ans.end()); 110 for(int i = 0,j = ans.size(); i < j; ++i) 111 printf("(%d,%d)%c",ans[i].first,ans[i].second,i+1==j?' ':','); 112 }else puts("not possible"); 113 } 114 } 115 return 0; 116 }
这样写 快很多啊
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 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 20000; 18 struct arc { 19 int to,flow,next; 20 arc(int x = 0,int y = 0,int z = -1) { 21 to = x; 22 flow = y; 23 next = z; 24 } 25 }; 26 arc e[maxn*20]; 27 int head[maxn],d[maxn],cur[maxn]; 28 int tot,S,T,N,E,C,cnt; 29 void add(int u,int v,int flow) { 30 e[tot] = arc(v,flow,head[u]); 31 head[u] = tot++; 32 e[tot] = arc(u,0,head[v]); 33 head[v] = tot++; 34 } 35 bool bfs() { 36 memset(d,-1,sizeof(d)); 37 queue<int>q; 38 d[T] = 1; 39 q.push(T); 40 while(!q.empty()) { 41 int u = q.front(); 42 q.pop(); 43 for(int i = head[u]; ~i; i = e[i].next) { 44 if(e[i^1].flow > 0 && d[e[i].to] == -1) { 45 d[e[i].to] = d[u] + 1; 46 q.push(e[i].to); 47 } 48 } 49 } 50 return d[S] > -1; 51 } 52 pii rec[maxn*20]; 53 int dfs(int u,int low) {aaa 54 if(u == T) return low; 55 int tmp = 0,a; 56 for(int &i = cur[u]; ~i; i = e[i].next) { 57 if(e[i].flow > 0&& d[u] == d[e[i].to]+1&&(a=dfs(e[i].to,min(low,e[i].flow)))) { 58 e[i].flow -= a; 59 e[i^1].flow += a; 60 low -= a; 61 tmp += a; 62 rec[cnt++] = make_pair(i,a); 63 rec[cnt++] = make_pair(i^1,-a); 64 if(!low) break; 65 } 66 } 67 if(!tmp) d[u] = -1; 68 return tmp; 69 } 70 int dinic() { 71 int ans = 0; 72 cnt = 0; 73 while(bfs()) { 74 memcpy(cur,head,sizeof(head)); 75 ans += dfs(S,INF); 76 } 77 return ans; 78 } 79 void release(){ 80 for(int i = 0; i < cnt; ++i) 81 e[rec[i].first].flow += rec[i].second; 82 } 83 vector< pii >ans; 84 int main() { 85 int u,v,w,cs = 1; 86 while(scanf("%d %d %d",&N,&E,&C),N||E||C) { 87 memset(head,-1,sizeof(head)); 88 S = 1; 89 T = N; 90 for(int i = 0; i < E; ++i) { 91 scanf("%d %d %d",&u,&v,&w); 92 add(u,v,w); 93 } 94 int flow = dinic(); 95 printf("Case %d: ",cs++); 96 if(flow >= C) puts("possible"); 97 else { 98 ans.clear(); 99 for(int i = 0; i < tot; i += 2) { 100 if(e[i].flow == 0){ 101 e[i].flow = C; 102 if(flow + dinic() >= C) ans.push_back(make_pair(e[i^1].to,e[i].to)); 103 release(); 104 e[i].flow = 0; 105 } 106 } 107 if(ans.size()){ 108 printf("possible option:"); 109 sort(ans.begin(),ans.end()); 110 for(int i = 0,j = ans.size(); i < j; ++i) 111 printf("(%d,%d)%c",ans[i].first,ans[i].second,i+1==j?' ':','); 112 }else puts("not possible"); 113 } 114 } 115 return 0; 116 }