对于每个输入的站点求出所有点到这个站点的最短路。用anss数组存下来,然后就可以用anss数组求出答案了。
题目分析清楚了 还是比较水的,折腾了一早上。。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
struct Point{ int tott, node; };
vector<int> abc[];
int nz, nr, i, j, p, k, u, summ, ii;
int zhuan[];
int fan[];//
int anss[][];//存最短路
int ff[];//标记这个城市有没有搜过最短路
void BFS(int rr)
{
queue<Point>Q; Point start, t;
start.node = zhuan[k]; start.tott = ; anss[rr][zhuan[k]] = ; Q.push(start);
while (!Q.empty())
{
Point hd = Q.front(); Q.pop();
for (i = ; i < abc[hd.node].size(); i++)
{
if (hd.tott + < anss[rr][abc[hd.node][i]])
{
anss[rr][abc[hd.node][i]] = hd.tott + ;
t.node = abc[hd.node][i];
t.tott = hd.tott + ;
Q.push(t);
}
}
}
}
int main()
{
int sb;scanf("%d", &sb);
while (sb--)
{
scanf("%d%d", &nz, &nr);
for (i = ; i < ; i++) abc[i].clear();
for (i = ; i < ; i++) for (j = ; j < ; j++) anss[i][j] = 0x7FFFFFFF;
memset(zhuan, , sizeof(zhuan));memset(ff, , sizeof(ff));
int tot = , summ = ;
for (i = ; i <= nz; i++)
{
scanf("%d", &p);
if (zhuan[p] == ) { zhuan[p] = tot, fan[zhuan[p]] = p, tot++; }
scanf("%d", &k);
for (j = ; j <= k; j++)
{
scanf("%d", &u);
if (zhuan[u] == ) { zhuan[u] = tot, fan[zhuan[u]] = u, tot++; }
abc[zhuan[p]].push_back(zhuan[u]);
}
}
for (ii = ; ii <= nr; ii++)
{
scanf("%d", &u);
for (j = ; j <= u; j++)
{
scanf("%d", &k);
if (ff[k] == ){ BFS(summ); ff[k] = ; summ++; }
}
}
int pri = 0x7FFFFFFF; int pri2 = -;
for (i = ; i <= tot - ; i++)
{
int minn = -;
for (j = ; j < summ; j++) if (anss[j][i]>minn) minn = anss[j][i];
if (minn < pri){ pri = minn, pri2 = fan[i]; }
else if (minn == pri&&fan[i] < pri2)pri2 = fan[i];
}
printf("%d %d\n", pri + , pri2);
}
return ;
}