Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream fin("modernizare.in");
- ofstream fout("modernizare.out");
- struct val{
- int nmin, nmax, cost;
- };
- bool comp(val a, val b){
- if(a.nmin == b.nmin){
- if(a.nmax == b.nmax)
- return a.cost < b.cost;
- return a.nmax < b.nmax;
- }
- return a.nmin < b.nmin;
- }
- int main(){
- int n, m, fond; fin >> n >> m >> fond;
- vector<int> nod[n + 1];
- vector<val> muchie;
- int viz[n + 1];
- for(int i = 1; i <= m; i++){
- int x, y, c; fin >> x >> y >> c;
- viz[x] = viz[y] = 0;
- nod[x].push_back(y); nod[y].push_back(x);
- muchie.push_back({x, y, c});
- }
- int coada[n + 1], ultim = 0, prim = 1; coada[++ultim] = 1; viz[1] = 1;
- while(prim <= ultim){
- int cur = coada[prim];
- for(auto i: nod[cur])
- if(viz[i] == 0){
- coada[++ultim] = i;
- viz[i] = viz[cur] + 1;
- }
- prim++;
- }
- for(auto i: muchie){
- i.nmin = min(viz[i.nmin], viz[i.nmax]);
- i.nmax = max(viz[i.nmin], viz[i.nmax]);
- }
- sort(muchie.begin(), muchie.end(), comp);
- int s = 0;
- while(!muchie.empty() && fond - muchie[0].cost >= 0){
- s++;
- fond -= muchie[0].cost;
- muchie.erase(muchie.begin());
- }
- fout << s;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement