不难,相同种类的距离是否为零用并查集判断。之后用floyd判断
int ind[600], father[MAXN], dis[600][600];
int number, kase, type, a, b, c;
void init()
{
REP(i, MAXN)
father[i] = i;
}
int getfather(int n)
{
return father[n] == n ? n : father[n] = getfather(father[n]);
}
void merge(int a, int b)
{
int fa = getfather(a), fb = getfather(b);
father[fb] = fa;
}
int gettype(int n)
{
return lower_bound(ind, ind + type, n) - ind;
}
int main()
{
// freopen("in.txt", "r", stdin);
while (~RIII(number, kase, type))
{
init();
CLR(dis, INF);
FE(i, 1, type)
{
RI(ind[i]);
ind[i] += ind[i - 1];
}
FE(i, 1, kase)
{
RIII(a, b, c);
if (c == 0)
merge(a, b);
int ta = gettype(a), tb = gettype(b);
if (ta != tb)
dis[ta][tb] = dis[tb][ta] = min(dis[ta][tb], c);
}
FE(i, 1, type)
{
int t = getfather(ind[i]);
FF(j, ind[i - 1] + 1, ind[i])
{
if (getfather(j) != t)
{
puts("No");
goto end;
}
}
}
puts("Yes");
FE(i, 1, type)
dis[i][i] = 0;
//
FE(k, 1, type) FE(i, 1, type) FE(j, 1, type)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
FE(i, 1, type)
{
FE(j, 1, type)
cout << (dis[i][j] == INF ? -1 : dis[i][j]) << ' ';
cout << endl;
}
//
end:;
}
return 0;
}