Pagini recente » Cod sursa (job #2211836) | Cod sursa (job #552525)
Cod sursa(job #552525)
// http://infoarena.ro/problema/critice
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define maxSize 1001
#define INFINITY 0x3f3f3f3f
#define source 1
ifstream in("critice.in");
ofstream out("critice.out");
int destination,edges;
bool visit[maxSize];
int father[maxSize];
int capacity[maxSize][maxSize];
int currentFlow[maxSize][maxSize];
vector<int> graph[maxSize];
vector< pair<int,int> > edge;
vector<int> critical;
bool breadthFirst();
int maxFlow();
int main() {
in >> destination >> edges;
int from,to,flow;
for(int i=1;i<=edges;i++) {
in >> from >> to >> flow;
capacity[from][to] = flow;
capacity[to][from] = flow;
edge.push_back(make_pair(from,to));
graph[from].push_back(to);
graph[to].push_back(from);
}
int initialFlow = maxFlow();
vector< pair<int,int> >::iterator it,end;
end = edge.end();
for(it=edge.begin();it!=end;++it)
if(capacity[it->first][it->second] == abs(currentFlow[it->first][it->second]) || capacity[it->first][it->second] == abs(currentFlow[it->second][it->first])) {
capacity[it->first][it->second] += 100000;
capacity[it->second][it->first] += 100000;
int newFlow = maxFlow();
if(newFlow > initialFlow)
critical.push_back(it - edge.begin() + 1);
capacity[it->first][it->second] -= 100000;
capacity[it->second][it->first] -= 100000;
}
out << critical.size() << "\n";
vector<int>::iterator sit,send;
send = critical.end();
for(sit=critical.begin();sit!=send;++sit)
out << *sit << "\n";
in.close();
out.close();
return (0);
}
bool breadthFirst() {
memset(visit,false,sizeof(visit));
visit[source] = true;
int node;
vector<int>::iterator it,end;
queue<int> myQueue;
myQueue.push(source);
while(!myQueue.empty()) {
node = myQueue.front();
myQueue.pop();
if(node == destination)
continue;
end = graph[node].end();
for(it=graph[node].begin();it!=end;++it)
if(!visit[*it] && currentFlow[node][*it] < capacity[node][*it]) {
visit[*it] = true;
myQueue.push(*it);
father[*it] = node;
}
}
return visit[destination];
}
int maxFlow() {
int maxFlow = 0;
vector<int>::iterator it,end;
for(int i=1;i<=destination;i++)
memset(currentFlow[i],0,sizeof(currentFlow[i]));
while(breadthFirst()) {
end = graph[destination].end();
for(it=graph[destination].begin();it!=end;++it)
if(visit[*it] && capacity[*it][destination] != currentFlow[*it][destination]) {
int minFlow = INFINITY;
father[destination] = *it;
for(int node=destination;node!=source;node=father[node])
minFlow = min(minFlow,capacity[father[node]][node] - currentFlow[father[node]][node]);
if(minFlow) {
for(int node=destination;node!=source;node=father[node]) {
currentFlow[father[node]][node] += minFlow;
currentFlow[node][father[node]] -= minFlow;
}
maxFlow += minFlow;
}
}
}
return maxFlow;
}