forked from oeddyo/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1794.cpp
More file actions
182 lines (142 loc) · 3.71 KB
/
1794.cpp
File metadata and controls
182 lines (142 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <algorithm>
#include <queue>
#include <list>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define DEBUG
#define REP(i,a) for(i=0;i<a;i++)
#define FOR(i,a,b) for(i=a;i<b;i++)
#define VE vector<int>
#define SZ size()
#define PB push_back
int N;
int sink;
const int MAXN = 10005*3, INF = 99999999;
typedef pair<int, int> pii;
vector< pii > graph[MAXN];
vector< pii > residual[MAXN];
bool visited[MAXN];
int pre[MAXN];
pii* get(int a, int b){
for(int i=0; i<residual[a].size(); i++){
if(a==1 && b == 2){
cout<<"special "<<residual[a][i].first<<" "<<residual[a][i].second<<endl;
}
if(residual[a][i].first == b){
return &residual[a][i];
}
}
return NULL;
}
bool bfs(){
memset(visited, 0, sizeof(visited));
memset(pre, -1, sizeof(pre));
visited[0] = true;
queue<int> Q;
Q.push(0);
bool path = false;
while(!Q.empty()){
int cur = Q.front();
Q.pop();
cout<<"now at "<<cur<<endl;
if(cur==sink){
path = true;
break;
}
for(int i=0; i<residual[cur].size(); i++){
cout<<"looking at "<<i<<endl;
if(residual[cur][i].second <= 0)continue;
cout<<"pass"<<endl;
int next = residual[cur][i].first;
if(visited[next]==false){
Q.push(next);
visited[next] = true;
pre[next] = cur;
}
}
}
cout<<"out"<<endl;
cout<<"path = "<<path<<endl;
if(path==false)return false;
usleep(3000);
int cur = sink;
int weight = INF;;
while(pre[cur]!=-1){
cout<<"back at "<<cur<<endl;
int p = pre[cur];
pii* tmp = get(p, cur);
cout<<"weight of "<<p<<" to "<<cur<<" = "<<tmp->second<<endl;
weight = min(weight, tmp->second);
cur = pre[cur];
}
cout<<"my weight = "<<weight<<endl;
cur = sink;
while(pre[cur]!=-1){
cout<<"ok im at "<<cur<<endl;
int p = pre[cur];
//cout<<"res = "<<residual[p][cur].second<<" "<<residual[cur][p].second<<endl;
pii* tmp_a = get(p, cur);
tmp_a->second -= weight;
pii* tmp_b = get(cur, p);
tmp_b->second += weight;
cout<<"kk "<<tmp_a->second<<" "<<tmp_b->second<<endl;
//residual[p][cur].second -= weight;
//residual[cur][p].second += weight;
cur = pre[cur];
}
return true;
}
void work(){
while(1){
bool ok = bfs();
if(ok == false)break;
}
cout<<"printing network"<<endl;
for(int i=1; i<=N; i++){
for(int j=0; j<residual[i].size(); j++){
cout<<i<<" "<<residual[i][j].first<<" "<<residual[i][j].second<<endl;
}
}
cout<<"ready"<<endl;
cout<<"N = "<<N<<endl;
for(int i=1; i<=N; i++){
pii* tmp = get(N+1, i);
cout<<"check "<<i<<" "<<N+1<<endl;
if(tmp==NULL){
cout<<"null "<<endl;
continue;
}
if(tmp->second!= 0){
cout<<"ans = "<<endl;
cout<<tmp->first<<endl;
}
}
}
int main(){
cin>>N;
sink = N*2+1;
cout<<"sink = "<<sink<<endl;
for(int i=1; i<=N; i++){
int t;
cin>>t;
t = t + N;
graph[i].push_back(pii(t, INF) );
graph[t].push_back(pii(i, 0));
graph[0].push_back(pii(i, 1));
graph[i].push_back(pii(0, 0));
graph[t].push_back(pii(sink, 1));
graph[sink].push_back(pii(t, 0));
}
for(int i=0; i<=sink; i++){
residual[i] = graph[i];
}
cout<<"ok here"<<endl;
work();
return 0;
}