| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:看了各位大虾的提示,终于AC了……贡献了3次wa……顺便问下0ms怎么出来的啊?In Reply To:看了各位大虾的提示,终于AC了……贡献了3次wa……顺便问下0ms怎么出来的啊? Posted by:Web_Board at 2009-03-21 15:40:09 // min_cost[i] record the min distance from the node[i] to U;
// flag[i] records the status of node[i](belong the set U or not);
#include <iostream>
using namespace std;
int matrix[101][101];
int min_cost[101];
int flag[101];
int n = 0;
void process()
{
__int64 cost = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
scanf("%d",&matrix[i][j]);
}
}
for (int i = 1; i <= n; ++i)
{
min_cost[i] = matrix[i][1];
flag[i] = 0;
}
flag[1] = 1;
for (int i = 2; i <= n; ++i)
{
int cur_min = 1000000;
int sub = -1;
for (int j = 2;j <= n; ++j)
{
if (cur_min > min_cost[j] && flag[j] == 0)
{
cur_min = min_cost[j];
sub = j;
}
}
cost += min_cost[sub];
flag[sub] = 1;
// flash min_cost;
for (int j = 2; j <= n; ++j)
{
if (flag[j] == 0 && min_cost[j] > matrix[j][sub])
{
min_cost[j] = matrix[j][sub];
}
}
}
printf("%I64d\n",cost);
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
process();
}
return 0;
}
/*感觉和你的一样,不过测试确是0MS,之前我用的是cin ,cout,时间是47MS,现在换成scanf和printf之后,就变成0MS*/
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator