Pagini recente » Cod sursa (job #1470861) | Cod sursa (job #2353776) | Cod sursa (job #2921236) | Cod sursa (job #1972134) | Cod sursa (job #2324224)
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <limits.h>
#include <limits.h>
using namespace std;
int N,M,total=0,nr_m=-1;
double a[9999][9999];
int minCost(int cost[],bool nodparcurs[]){
int minim=INT_MAX,muchie_min;
for(int i=1;i<=N;i++){
if(nodparcurs[i]==false && cost[i]<minim){
minim=cost[i];
muchie_min=i;
}
}
return muchie_min;
}
void afisare(int parent[]){
ofstream o;
o.open("apm.out");
o<<total<<endl;
o<<nr_m<<endl;
for(int i=2;i<=N;i++){
o<<parent[i]<<" "<<i<<endl;
}
}
void parcurgere(double a[9999][9999]){
int parent[200001];
int cost[200001];
bool nodparcurs[200001];
for(int i=1;i<=N;i++){
cost[i]=INT_MAX;
nodparcurs[i]=false;
}
cost[1]=0;
parent[1]=0;
for(int i=1;i<=N;i++){
int k=minCost(cost,nodparcurs);
nodparcurs[k]=true;
total=total+cost[k];
nr_m++;
for(int j=1;j<=N;j++){
if(a[k][j]!=0 && nodparcurs[j]==false && a[k][j]<cost[j]){
parent[j]=k;
cost[j]=a[k][j];
}
}
}
afisare(parent);
}
void except(){
ofstream o;
o.open("apm.out");
o<<0<<endl;
o<<0;
}
int main()
{ int x,y;double c;
ifstream f;
f.open("apm.in");
f>>N;
f>>M;
for(int i=1;i<=M;i++){
f>>x>>y>>c;
a[x][y]=c;
a[y][x]=c;
}
if(M!=0){
parcurgere(a);
f.close();
}
else
except();
return 0;
}