题目地址:
http://acm.jlu.edu.cn/joj/showproblem.php?pid=1170
我的程序:
#include <iostream>
using namespace std;
const int maxv = 9999;
int main()
{
int m; //m张地图
cin >> m;
while (m --)
{
int n; //记录有多少个顶点
cin >> n;
int c[n+1][n+1];
bool s[n+1];
int lowcost[n+1];
int minsum = 0;
for (int i = 1;i < n+1;i ++)
for (int j = 1;j < n+1;j ++)
c[i][j] = maxv;
//输入各个有连接顶点之间的边权
int b,e,d;
while (cin >> b && cin >>e && cin >>d && b != 0 && e !=0 && d != 0)
c[b][e] = d;
s[1] = true;
for (int i = 2;i <= n+1;i ++)
{
lowcost[i] = c[1][i];
s[i] = false;
}
for (int i = 1;i < n;i ++)
{
int j;
int min = maxv;
for (int k = 2;k < n+1;k ++)
if ((!s[k]) && (lowcost[k] < min))
{
min = lowcost[k];
j = k;
}
s[j] = true;
for (int k =2;k < n+1;k ++)
if ((c[j][k] < lowcost[k]) && (!s[k]))
lowcost[k] = c[j][k];
minsum += lowcost[j];
}
cout << minsum << endl;
}
}
请高手指教,在线等