Pagini recente » Cod sursa (job #595106) | Cod sursa (job #2392434) | Cod sursa (job #3265622) | Cod sursa (job #3266207) | Cod sursa (job #2375644)
# include <fstream>
# include <vector>
# include <set>
# define DIM 200010
# define INF 1000000000
# define f first
# define s second
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
vector<pair<int,int> > Lista[DIM];
set<pair<int,int> > myset;
set<pair<int,int> >::iterator it;
int Marcat[DIM],t[DIM],d[DIM],sol,n,m,i,x,y,cost,nc,nv;
int main () {
fin>>n>>m;
for(i=1;i<=m;i++){
fin>>x>>y>>cost;
Lista[x].push_back(make_pair(y,cost));
Lista[y].push_back(make_pair(x,cost));
}
myset.insert(make_pair(-INF,1));
d[1]=-INF;
for(i=2;i<=n;i++)
d[i]=INF;
while(!myset.empty()){
it=myset.begin();
nc=it->s;
if(Marcat[nc]==0){
Marcat[nc]=1;
if(nc!=1)
sol+=it->f;
myset.erase(it);
}
else{
myset.erase(it);
continue;
}
for(i=0;i<Lista[nc].size();i++){
nv=Lista[nc][i].f;
cost=Lista[nc][i].s;
if(d[nv]>=cost&&Marcat[nv]==0){
d[nv]=cost;
t[nv]=nc;
myset.insert(make_pair(d[nv],nv));
}
}
}
fout<<sol<<"\n"<<n-1<<"\n";
for(i=2;i<=n;i++)
fout<<i<<" "<<t[i]<<"\n";
return 0;
}