Pagini recente » Cod sursa (job #2306477) | Cod sursa (job #2172723) | Cod sursa (job #2789500) | Cod sursa (job #920697) | Cod sursa (job #1804973)
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#define Pe pair <int, int>
#define mp make_pair
#define fi first
#define se second
using namespace std;
class InputReader {
public:
InputReader() {}
InputReader(const char *file_name) {
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
inline InputReader &operator >>(int &n) {
while (buffer[cursor] < '0' || buffer[cursor] > '9') {
advance();
}
n = 0;
while ('0' <= buffer[cursor] && buffer[cursor] <= '9') {
n = n * 10 + buffer[cursor] - '0';
advance();
}
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor;
char buffer[SIZE];
inline void advance() {
++ cursor;
if (cursor == SIZE) {
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
};
InputReader cin ("critice.in");
ofstream cout ("critice.out");
const int MaxN = 1005, Inf = 0x3f3f3f3f;
vector <Pe> Edg;
vector <int> Ans, G[MaxN];
queue <int> Q;
int n, m;
int Capacity[MaxN][MaxN], Flow[MaxN][MaxN], father[MaxN];
inline void ClearQ() {
while (Q.size()) {
Q.pop();
}
}
inline bool IsFree(Pe coord, int n1, int n2) {
return coord == mp(n1, n2) or coord == mp(n2, n1);
}
bool Bfs(Pe coord = mp(-1, -1), int StNode = 1) {
ClearQ();
memset(father, 0, sizeof father);
Q.push(StNode);
father[StNode] = -1;
while (Q.size()) {
int node = Q.front();
Q.pop();
for (auto i: G[node]) {
if (!father[i] and (Capacity[node][i] - Flow[node][i] or IsFree(coord, node, i))) {
Q.push(i);
father[i] = node;
if (i == n) {
return true;
}
}
}
}
return false;
}
inline int CalcUpdate(int node = n) {
int ans = Inf;
while (father[node] != -1) {
int parent = father[node];
ans = min(ans, Capacity[parent][node] - Flow[parent][node]);
node = parent;
}
return ans;
}
inline void FlowUpdate(int Quantity, int node = n) {
while (father[node] != -1) {
int parent = father[node];
Flow[parent][node] += Quantity;
Flow[node][parent] -= Quantity;
node = parent;
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int a, b, c;
cin >> a >> b >> c;
G[a].push_back(b);
G[b].push_back(a);
Capacity[a][b] = c;
Capacity[b][a] = c;
Edg.push_back(mp(a, b));
}
while (Bfs()) {
int Quantity = CalcUpdate();
FlowUpdate(Quantity);
}
for (int i = 0; i < m; ++i) {
if (Bfs(Edg[i])) {
Ans.push_back(i + 1);
}
}
cout << Ans.size() << '\n';
for (auto it: Ans) {
cout << it << '\n';
}
return 0;
}