POJ 2377

 #include<stdio.h>
#define MAXN 1005
#include<iostream>
#include<algorithm>
#define inf 10000000
using namespace std;
int _m[MAXN][MAXN];
int low_cost[MAXN];
int pre[MAXN];
unsigned prime(int n);
int DFS(int i,int sum,int p);
bool mark[MAXN];
int main()
{
//freopen("acm.acm","r",stdin);
int p;
int edge;
unsigned tem;
int i;
int j;
int u;
int v;
while(cin>>p>>edge)
{
memset(mark,false,sizeof(mark));
for(i = ; i < p; ++ i)
{
for(j = ; j < p; ++ j)
_m[i][j] = inf;
}
for(i = ; i < edge; ++ i)
{
cin>>u>>v;
-- u;
-- v;
cin>>tem;
if(_m[u][v] == inf)
{
_m[u][v] = tem;
_m[u][v] *= -;
_m[v][u] = _m[u][v];
}
else
if(tem > _m[u][v]*(-))
{
_m[u][v] = tem;
_m[u][v] *= -;
_m[v][u] = _m[u][v];
}
}
mark[] = true;
tem = -prime(p);
if(DFS(,,p) == p)
cout<<tem<<endl;
else
cout<<-<<endl;
}
}
int DFS(int i,int sum,int n)
{
int j;
for(j = ; j < n; ++ j)
{
if(i != j && _m[i][j] != inf && !mark[j])
{
mark[j] = true;
sum = DFS(j,sum+,n);
}
}
return sum;
}
unsigned prime(int n)
{
int i;
int j;
int k;
unsigned sum = ;
int min;
for(i = ; i < n; ++ i)
{
low_cost[i] = _m[][i];
pre[i] = ;
}
for(i = ; i < n; ++ i)
{
min = inf;
for(j = ; j < n; ++ j)
{
if(low_cost[j]&&low_cost[j] < min)
{
k = j;
min = low_cost[j];
}
}
sum += low_cost[k];
low_cost[k] = ;
for(j = ; j < n; ++ j)
{
if(_m[k][j] < low_cost[j] && low_cost[j])
{
low_cost[j] = _m[k][j];
pre[j] = k;
}
}
}
return sum;
}
上一篇:微信浏览器返回刷新,监听微信浏览器返回事件,网页防复制,移动端禁止图片长按和vivo手机点击img标签放大图片


下一篇:JavaScript监听手机物理返回键的两种解决方法