生成树
代码如下:
#include <bits/stdc++.h>
using namespace std;
int mat[2][110][110];
const int inf = 1e9;
int vis[110], dis[110], n;
int dij(int dex)
{
memset(dis, 0x3f3f3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[0] = 0;
int ret = 0;
for(int i = 0;i <= n;i++)
{
int u = -1, mi = inf;
for(int j = 0;j <= n;j++)
if(!vis[j] && dis[j] < mi)
{
mi = dis[j];
u = j;
}
if(u == -1)
break;
ret += dis[u];
vis[u] = 1;
for(int j = 0;j <= n;j++)
{
if(!vis[j] && dis[j] > mat[dex][u][j])
dis[j] = mat[dex][u][j];
}
}
return ret;
}
int main ()
{
int t, cnt = 1;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
int u, v, w;
memset(mat, 0x3f3f3f, sizeof(mat));
while(scanf("%d %d %d", &u, &v, &w), u || v || w)
{
mat[0][u][v] = mat[0][v][u] = min(mat[0][u][v], w);
mat[1][u][v] = mat[1][v][u] = min(mat[1][u][v], -w);
}
int ans = dij(0) + (-dij(1));
printf("Case %d: ", cnt++);
if(ans%2) printf("%d/2\n", ans);
else printf("%d\n", ans/2);
}
return 0;
}