Pagini recente » Cod sursa (job #2949276) | Cod sursa (job #2872400) | Cod sursa (job #2390039) | Cod sursa (job #2656083) | Cod sursa (job #2695324)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");
int n,m;
vector<int> firstNode, secondNode;
vector<int> edges[1001];
int graph[1001][1001];
int parent[1005];
queue <int> q;
bool visited[1005];
int bfs()
{
int s = 1;
int t = n;
memset(parent, 0, sizeof(parent));
q.push(s);
parent[s] = -1;
while (!q.empty())
{
int node = q.front();
q.pop();
for (auto nextNode : edges[node])
{
if (!parent[nextNode] && graph[node][nextNode] > 0)
{
q.push(nextNode);
parent[nextNode] = node;
}
}
}
return parent[t];
}
void dfs (int s)
{
visited[s] = true;
for (auto& nextNode : edges[s])
if (graph[s][nextNode] && !visited[nextNode])
dfs(nextNode);
}
void solve ()
{
int node, path_flow;
while (bfs())
{
for (auto& nextNode : edges[n])
if (parent[nextNode] && graph[nextNode][n])
{
node = n;
parent[n] = nextNode;
while (node != 1)
{
path_flow = min(path_flow, graph[parent[node]][node]);
node = parent[node];
}
node = n;
if (path_flow > 0)
while (node != 1)
{
graph[parent[node]][node] -= path_flow;
graph[node][parent[node]] += path_flow;
node = parent[node];
}
}
}
dfs(1);
int ans = 0;
for (int i = 1; i <= m; i++)
if (visited[firstNode[i]] != visited[secondNode[i]])
ans ++;
fout << ans << "\n";
for (int i = 1; i<= m; i++)
if (visited[firstNode[i]] != visited[secondNode[i]])
fout << i << "\n";
}
int main()
{
fin >> n >> m;
firstNode.resize(m + 5);
secondNode.resize(m + 5);
for(int i = 1; i <= m; ++i)
{
int c;
fin >> firstNode[i] >> secondNode[i] >> c;
edges[firstNode[i]].push_back(secondNode[i]);
edges[secondNode[i]].push_back(firstNode[i]);
graph[firstNode[i]][secondNode[i]] = c;
graph[secondNode[i]][firstNode[i]] = c;
}
solve();
return 0;
}