Pagini recente » Cod sursa (job #3161771) | Cod sursa (job #3181493) | Cod sursa (job #2892510) | Cod sursa (job #634363) | Cod sursa (job #2951303)
#include<iostream>
#include<deque>
#include<algorithm>
#include<vector>
#include<fstream>
#include<queue>
using namespace std;
ifstream in("harta.in");
ofstream out("harta.out");
struct edge{
int a,b,capacity;
};
const int INF = 2e9;
const int mx = 230;
int n;
int num_nodes;
vector<int> ind,outd;
int capacity[mx][mx];
int flow[mx][mx];
vector<int> graph[mx];
void read(){
in>>n;
ind.resize(n+1);
outd.resize(n+1);
for(int i=1;i<=n;i++){
in>>outd[i]>>ind[i];
}
}
void add_edge(int a,int b,int cap){
graph[a].push_back(b);
graph[b].push_back(a);
capacity[a][b]=cap;
}
int bfs(int s, int t, vector<int>& parent) {
fill(parent.begin(), parent.end(), -1);
parent[s] = -2;
queue<pair<int, int>> q;
q.push({s, INF});
while (!q.empty()) {
int cur = q.front().first;
int flow = q.front().second;
q.pop();
for (int next : graph[cur]) {
if (parent[next] == -1 && capacity[cur][next]) {
parent[next] = cur;
int new_flow = min(flow, capacity[cur][next]);
if (next == t)
return new_flow;
q.push({next, new_flow});
}
}
}
return 0;
}
int maxflow(int s, int t) {
int flow = 0;
vector<int> parent(mx);
int new_flow;
while ((new_flow = bfs(s, t, parent))) {
flow += new_flow;
int cur = t;
while (cur != s) {
int prev = parent[cur];
capacity[prev][cur] -= new_flow;
capacity[cur][prev] += new_flow;
cur = prev;
}
}
return flow;
}
void solve(){
for(int i=1;i<=n;i++){
add_edge(0,i,outd[i]);
}
for(int i=1; i<=n;i++){
for(int j = 1;j<=n;j++){
if(i!=j){
add_edge(i,n+j,1);
}
}
}
for(int j=1;j<=n;j++){
add_edge(n+j,2*n+1,ind[j]);
}
int start=0,end=2*n+1;
out<<maxflow(start,end)<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j)
continue;
if(capacity[i][n+j]==0){
out<<i<<" "<<j<<endl;
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}