分析数据结构 是树形的
解题前, 都应该分析此题目的所采用的数据结构是什么, 会节省很多时间。
http://acm.hdu.edu.cn/showproblem.php?pid=1311
#include<bits/stdc++.h>
using namespace std;
typedef pair<string, string> ss;
typedef pair<int, ss> iss;
bool com(iss a, iss b){
if(a.first > b.first){
return true;
}else if(a.first == b.first && a.second.second < b.second.second){
return true;
}
return false;
}
int main()
{
//freopen("1.txt", "r", stdin);
int t;
cin >> t;
for(int k = 1; k<=t; k++){
int n;
cin >> n;
vector<iss> v;
for(int i = 0; i<n; i++){
string a, b;
int c;
cin >> a >> b >> c;
v.push_back(iss(c, ss(a, b)));
}
map<string, int> mp;
mp["Ted"] = 100;
queue<string> q;
q.push("Ted");
while(!q.empty()){
string data = q.front();
q.pop();
for(int j = 0; j<n; j++){
if(v[j].second.first == data){
mp[v[j].second.second] = mp[data] - v[j].first;
q.push(v[j].second.second);
}
}
}
for(int i = 0; i<n; i++){
v[i].first = mp[v[i].second.second];
}
sort(v.begin(), v.end(), com);
cout << "DATASET " << k << endl;
for(int i = 0; i<n; i++){
cout << v[i].second.second << " " << v[i].first << endl;
}
}
return 0;
}