Cod sursa(job #2444748)

Utilizator vladth11Vlad Haivas vladth11 Data 1 august 2019 12:22:42
Problema Lazy Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("lazy.in");
ofstream cout("lazy.out");
struct muchie{
    long long a,b,id;
    long long cost,profit;

    bool operator < (muchie const &a)const{
         if(a.cost != cost){
            return (cost < a.cost);
         }else{
            return (a.profit < profit);
         }
    }
}v[200001];
long long c[200001];
long long radacina(long long x){
    if(c[x] == x){
        return x;
    }
    c[x] = radacina(c[x]);
    return c[x];
}
void uneste(long long a,long long b){
    c[radacina(a)] = radacina(b);
}
int main()
{
    long long n,m,i;
    cin >> n >> m;
    for(i = 1;i <= n;i++){
        c[i] = i;
    }
    for(i = 1;i <= m;i++){
        cin >> v[i].a >> v[i].b >> v[i].cost >> v[i].profit;
        v[i].profit *= v[i].cost;
        v[i].id = i;
    }
    sort(v + 1,v + m + 1);

    long long cnt = 0;
    for(i = 1;i <= m;i++){
        if(radacina(v[i].a) != radacina(v[i].b)){
            cout << v[i].id << "\n";
            uneste(v[i].a,v[i].b);
        }
    }

    return 0;
}