唉,看错了,写成了克鲁斯卡尔算法,尴尬
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<iostream>
using namespace std;
int b[20];struct node
{char x, y;int date;
}a[100];void init()
{for (int i = 0; i < 20; i++){b[i] = i;}
}bool cmp(node x, node y)
{if (x.date < y.date)return true;return false;
}int find(int x)
{if (b[x] == x)return x;return b[x] = find(b[x]);
}int main()
{int n, m;while (cin >> n>>m){char ch[20];cin >> ch;init();for (int i = 0; i < m; i++){cin >> a[i].x >> a[i].y >> a[i].date;}sort(a, a + m, cmp);int k = 0;for (int i = 0; i < m; i++){if (k == n - 1)break;int x, y;for (int j = 0; j < n; j++){if (a[i].x == ch[j])x = j + 1;if (a[i].y == ch[j])y = j + 1;}if (find(x) != find(y)){cout << "(" << a[i].x << "," << a[i].y << ")";k++;b[find(x)] = b[find(y)];}}}return 0;
}