Cod sursa(job #3198327)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 28 ianuarie 2024 21:09:51
Problema Pitici Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("pitici.in");
ofstream cout("pitici.out");
using pii = pair<int,int>;
const int nmax = 1020 + 1;
bool mark[200500];
int n , k , m , a , b , c , pre[nmax] , pret[nmax] , we[nmax] , wet[nmax];
long long dp[nmax] , dpt[nmax];
struct edge{ int y , c, idx;};
struct muchie{int x , y , c, idx;};
vector<muchie>ed;
vector<edge>g[nmax];
vector<edge>t[nmax];
struct cmp
{
    bool operator()(int one , int two)
    {
        return dp[one] > dp[two];
    }
};
struct cmp2
{
    bool operator()(int one , int two)
    {
        return dpt[one] > dpt[two];
    }
};
priority_queue<int,vector<int>,cmp>pq;
priority_queue<int,vector<int>,cmp2>pqt;
int main()
{
    cin >> n >> m >> k;
    ed.push_back({0,0,0});
    for(int i = 1 ; i <= m ; ++i)
    {
        cin >> a >> b >> c;
        g[a].push_back({b,c,i});
        t[b].push_back({a,c,i});
        ed.push_back({a,b,c,i});
    }
    for(int i = 1 ; i <= n ; ++i)
    {
        dp[i] = 1e9;
        dpt[i] = 1e9;
    }
    dp[1] = 0;
    pq.push(1);
    while(!pq.empty())
    {
        a = pq.top();
        pq.pop();
        for(auto it : g[a])
        {
            if(dp[it.y] > dp[a] + it.c)
            {
                pre[it.y] = a;
                we[it.y] = it.idx;
                dp[it.y] = dp[a] + it.c;
                pq.push(it.y);
            }
        }
    }
    dpt[n] = 0;
    pqt.push(n);
    while(!pqt.empty())
    {
        a = pqt.top();
        pqt.pop();
        for(auto it : t[a])
        {
            if(dpt[it.y] > dpt[a] + it.c)
            {
                pret[it.y] = a;
                wet[it.y] = it.idx;
                dpt[it.y] = dpt[a] + it.c;
                pqt.push(it.y);
            }
        }
    }
    sort(ed.begin(),ed.end(),[](muchie a , muchie b)
    {return dp[a.x] + dpt[a.y] + a.c < dp[b.x] + dpt[b.y] + b.c;});
    for(int i = 1 ; i <= m && k ; ++i)
    {
        muchie e = ed[i];
        if(!mark[e.idx])
        {
            mark[e.idx] = 1;
            k--;
            cout << dp[e.x] + dpt[e.y] + e.c << ' ';
            int a = e.x;
            while(pre[a])
            {
                mark[we[a]] = 1;
                a = pre[a];
            }
            a = e.y;
            while(pret[a])
            {
                mark[wet[a]] = 1;
                a = pret[a];
            }
        }
    }
    return 0;
}