Pagini recente » Cod sursa (job #576863) | Cod sursa (job #2313984) | Cod sursa (job #1628111) | Cod sursa (job #342823) | Cod sursa (job #663769)
Cod sursa(job #663769)
#include <fstream>
#include <algorithm>
#include <cstring>
#define vL 10001
#define eL 10001
#define tL 201
using namespace std;
ifstream in;
ofstream out;
struct edge
{
int x,y,c;
}v[vL];
int t[tL];
char use[tL][tL];
int rang[tL];
inline bool cmp(const edge &a,const edge &b)
{
return a.c<b.c;
}
int main()
{
int M,N,K,x,y,c;
in.open("online.in");
in>>N>>M;
for(int i=1;i<=M;++i) in>>v[i].x>>v[i].y>>v[i].c;
sort(v+1,v+M+1,cmp);
for(int i=1;i<=N;++i) t[i]=i;
memset(use,0,sizeof(use));
int cost=0;
memset(rang,0,sizeof(rang));
int cnt=0;
for(int i=1;i<=M;++i)
{
while(t[v[i].x]!=t[t[v[i].x]]) t[v[i].x]=t[t[v[i].x]];
while(t[v[i].y]!=t[t[v[i].y]]) t[v[i].y]=t[t[v[i].y]];
if(t[v[i].x]!=t[v[i].y])
{
if(rang[t[v[i].x]]<rang[t[v[i].y]]) t[t[v[i].y]]=t[v[i].x], rang[t[v[i].y]]++;
else t[t[v[i].x]]=t[v[i].y], rang[t[v[i].x]]++;
cost+=v[i].c;
use[v[i].x][v[i].y]=v[i].c;
use[v[i].y][v[i].x]=v[i].c;
}
}
out.open("online.out");
out<<cost<<'\n';
int cmax,mx,my;
in>>K;
for(;K--;)
{
in>>x>>y>>c;
if(use[x][y]==0)
{
cmax=0;
for(int i=1;i<=N;++i)
{
if(use[x][i]>cmax)
{
cmax=use[x][i];
mx=x;
my=i;
}
if(use[y][i]>cmax)
{
cmax=use[y][i];
mx=y;
my=i;
}
}
if(cmax<=c) continue;
cost-=cmax;
cost+=c;
use[mx][my]=0;
use[my][mx]=0;
use[x][y]=c;
use[y][x]=c;
}
else
if(use[x][y]>c)
{
cost-=use[x][y];
use[x][y]=c;
use[y][x]=c;
cost+=c;
}
out<<cost<<'\n';
}
in.close();
out.close();
return 0;
}