这题关键不在于谁把消息发送出去,而是谁收到消息是什么时间。
对于每个发送目标去重后用bfs搜索即可
#include<iostream>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
struct node {
int des, time;
};
int bfs(vector<vector<node> > inf, vector<node> cur, int n, int start) {
int sum = 0;
set<int> all;
for (int i = 1; i <= n; i++) {
if (i == start)continue;
all.insert(i);
}
while (!all.empty()) {
int mintime = 100;
vector<int> newNode;
while (newNode.empty()) {
mintime = 100;
for (int i = 0; i < cur.size(); i++) {
if (mintime > cur[i].time) {
mintime = cur[i].time;
}
}
for (vector<node>::iterator it = cur.begin(); it != cur.end();) {
int time = (*it).time;
int des = (*it).des;
if (time == mintime) {
if (all.find(des) != all.end()) {
all.erase(des);
newNode.push_back(des);
}
it = cur.erase(it);
continue;
}
it++;
}
if (cur.empty()&&!all.empty()) return -1;
}
sum += mintime;
for (int i = 0; i < cur.size(); i++) {
cur[i].time -= mintime;
}
for (int i = 0; i < newNode.size(); i++) {
vector<node> nds = inf[newNode[i]];
for (int j = 0; j < nds.size(); j++) {
if (all.find(nds[j].des) != all.end()) {
cur.push_back(nds[j]);
}
}
}
}
return sum;
}
int main() {
int n, m;
while (cin >> n && n) {
vector<vector<node> > ve;
vector<node> t;
ve.push_back(t);
for (int i = 1; i <= n; i++) {
cin >> m;
vector<node> t;
for (int j = 1; j <= m; j++) {
node n;
cin >> n.des >> n.time;
t.push_back(n);
}
ve.push_back(t);
}
int minsum = 100000;
int index;
for (int i = 1; i <= n; i++) {
vector<node> cur;
for (int j = 0; j < ve[i].size(); j++) {
cur.push_back(ve[i][j]);
}
int ans = bfs(ve, cur, n, i);
if (ans >= 0 && minsum > ans) {
minsum = ans;
index = i;
}
}
if (minsum == 100000) {
cout << "disjoint'" << endl;
} else {
cout << index << " " << minsum << endl;
}
}
}