Cod sursa(job #2839721)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 26 ianuarie 2022 14:34:07
Problema Traseu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.94 kb
#include <bits/stdc++.h>
//#pragma GCC optimize ("03")
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("traseu.in" , "r" , stdin) , freopen("traseu.out" , "w" , stdout)
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < int , int >
#define dbg(x) cerr << #x << ": " << x << '\n'

namespace FastRead
{
    char __buff[5000];int __lg = 0 , __p = 0;
    char nc()
    {
        if(__lg == __p){__lg = fread(__buff , 1 , 5000 , stdin);__p = 0;if(!__lg) return EOF;}
        return __buff[__p++];
    }
    template<class T>void read(T&__x)
    {
        T __sgn = 1; char __c;while(!isdigit(__c = nc()))if(__c == '-')__sgn = -1;
        __x = __c - '0';while(isdigit(__c = nc()))__x = __x * 10 + __c - '0';__x *= __sgn;
    }
}

using namespace FastRead;
using namespace std;

const int N = 65;
const int M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n , m , sink , source , x , y , z;
int capacity[N][N] , cost[N][N] , parent[N] , inDeg[N] , outDeg[N] , d[N] , dist[N];
bool inq[N];
vector < int > G[N];
queue < int > q;
priority_queue < pii , vector < pii > , greater < pii > > pq;

void addEdge(int x , int y , int c , int z)
{
    G[x].pb(y);
    G[y].pb(x);

    cost[x][y] = z;
    cost[y][x] = -z;

    capacity[x][y] = c;
    capacity[y][x] = 0;
}

void bellman()
{
    for(int i = 1 ; i <= n + 1 ; i++)
        dist[i] = INT_MAX;

    dist[source] = 0;
    q.push(source);
    inq[source] = 1;

    while(!q.empty())
    {
        int node = q.front(); q.pop();
        inq[node] = 0;

        for(auto to : G[node])
            if(capacity[node][to] && dist[node] + cost[node][to] < dist[to])
            {
                dist[to] = dist[node] + cost[node][to];
                if(!inq[to]) q.push(to);
                inq[to] = 1;
            }
    }
}

bool djikstra()
{
    for(int i = 1 ; i <= n + 1 ; i++)
        d[i] = INT_MAX;

    d[source] = 0;
    pq.push({d[source] , source});

    while(!pq.empty())
    {
        int node = pq.top().second;
        int curr_dist = pq.top().first;

        pq.pop();

        if(curr_dist != d[node]) continue;

        for(auto to : G[node])
            if(capacity[node][to] && d[node] + dist[node] + cost[node][to] - dist[to] < d[to])
            {
                d[to] = dist[node] + cost[node][to] - dist[to] + d[node];
                parent[to] = node;
                pq.push({d[to] , to});
            }
    }

    return d[sink] != INT_MAX;
}

int Flow()
{
    int flowCost = 0;

    while(djikstra())
    {
        int minFlow = INT_MAX;

        for(int curr = sink ; curr != source ; curr = parent[curr])
            minFlow = min(minFlow , capacity[parent[curr]][curr]);

        for(int curr = sink ; curr != source ; curr = parent[curr])
        {
            capacity[parent[curr]][curr] -= minFlow;
            capacity[curr][parent[curr]] += minFlow;
            flowCost += minFlow * cost[parent[curr]][curr];
        }
    }

    return flowCost;
}

signed main()
{
	#ifndef ONLINE_JUDGE
		FastIO , FILES;
	#endif

	int i , ans = 0;

    cin >> n >> m;

    while(m--)
    {
        cin >> x >> y >> z;

        ans += z;

        outDeg[x]++;
        inDeg[y]++;

        addEdge(x , y , INT_MAX , z);
    }

    sink = n + 1;
    source = 0;

    for(i = 1 ; i <= n ; i++)
        if(inDeg[i] > outDeg[i])
            addEdge(source , i , inDeg[i] - outDeg[i] , 0);
        else if(inDeg[i] < outDeg[i])
            addEdge(i , sink , outDeg[i] - inDeg[i] , 0);


    bellman();
    ans += Flow();
    cout << ans;

    return 0;
}