Ideas:
Because I used a question called "smooth engineering" (hdoj1233), and the question was almost the same. I used the split algorithm,
Only judgment statements are added to the original program,
If (min = 1000000 ){
Cout <'? '<Endl;
Return;
It seems that some minor adjustments have been made, so the ac is ready. Haha.
[Cpp]
// Hdoj1863
# Include <iostream>
Using namespace std;
Int path [101] [101];
Int flag [101];
Int closepath [101];
Int sum;
Void prim (int x, int n)
{
Int I, count = n-1, pose = x;
Flag [x] = 1;
For (I = 1; I <= n; I ++)
Closepath [I] = path [x] [I];
While (count)
{
Int min = 1000000;
For (I = 1; I <= n; I ++)
If (min> closepath [I] &! Flag [I])
{
Min = closepath [I];
Pose = I;
}
If (min = 1000000 ){
Cout <'? '<Endl;
Return;
}
Count --; sum + = min;
Flag [pose] = 1;
For (I = 1; I <= n; I ++)
If (closepath [I]> path [pose] [I] &! Flag [I])
Closepath [I] = path [pose] [I];
}
Cout <sum <endl;
}
Void main ()
{
Int n, m, I, j, begin, end, len;
While (cin> n, n) // number of roads
{
Cin> m; // number of villages
Memset (flag, 0, sizeof (flag ));
Sum = 0;
For (I = 1; I <= m; I ++)
For (j = 1; j <= m; j ++)
{
If (I = j)
Path [I] [j] = 0;
Else
Path [I] [j] = 1000000;
}
For (I = 1; I <= n; I ++)
{
Cin> begin> end> len;
If (len <path [begin] [end]) //
Path [begin] [end] = path [end] [begin] = len;
}
Prim (1, m );
}
}