比赛 |
名字我取了 |
评测结果 |
WWWWWTTTTT |
题目名称 |
最好的边权 |
最终得分 |
0 |
用户昵称 |
胡嘉兴 |
运行时间 |
10.015 s |
代码语言 |
C++ |
内存使用 |
19.39 MiB |
提交时间 |
2017-09-15 20:28:28 |
显示代码纯文本
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cctype>
#define N 1000000 + 7
#define INF 999999997
using namespace std;
int n, m, w[N], u[N], v[N], r[N], p[N];
int max(int a, int b)
{
return a > b ? a : b;
}
int cmp(const int i, const int j)
{
return w[i] < w[j];
}
int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}
int Kruskal(int z)
{
int ans = 0, book;
for(int i = 1; i <= n; i++)
{
p[i] = i;
}
for(int i = 1; i <= m; i++)
{
r[i] = i;
}
sort(r, r + m, cmp);
if(z)
{
for(int i = 1; i <= m; i++)
{
if(r[i] == z)
{
book = i;
break;
}
}
int e = r[book], x = find(u[e]), y = find(v[e]);
if(x != y)
{
ans += w[e];
p[x] = y;
}
r[book] = INF;
}
for(int i = 1; i <= m; i++)
{
int e = r[i];
if(e == INF)
{
continue;
}
int x = find(u[e]), y = find(v[e]);
if(x != y)
{
ans += w[e];
p[x] = y;
}
}
return ans;
}
int main()
{
int flag;
freopen("BEW.in", "r", stdin);
freopen("BEW.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
u[i] = a;
v[i] = b;
w[i] = c;
}
flag = Kruskal(0);
for(int i = 1; i <= m; i++)
{
if(n == m + 1)
{
printf("-1\n");
}
else
{
int z = Kruskal(i);
if(z > flag)
{
printf("%d\n", w[i] - z + flag - 1);
}
else if(z == flag)
{
printf("%d\n", w[i]);
}
}
}
return 0;
}