poj 2021

ac代码

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>

#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int

using namespace std;

struct Peo{
	int age;
	char name[20];
	bool operator<(const Peo &b) {
		if (age == b.age)return (strcmp(name, b.name) < 0);
		return age > b.age;
	}
}a[105];

int main(){
	char fa[105][20], ch[105][20];
	int c, t, n, year[105];

	cin >> c;
	a[0].age = 100;
	strcpy(a[0].name, "Ted");
	for (t = 1; t <= c; t++){
		cin >> n;
		for (int i = 1; i <= n; i++)
			cin >> fa[i] >> ch[i] >> year[i];

		for (int i = 0, k = 1; i < n; i++)
			for (int j = 1; j <= n; j++)
				if (strcmp(a[i].name, fa[j]) == 0) {
					a[k].age = a[i].age - year[j];
					strcpy(a[k].name, ch[j]);
					k++;
				}

		sort(a, a + n + 1);
		cout << "DATASET " << t << endl;
		for (int i = 1; i <= n; i++)
			cout << a[i].name << ' ' << a[i].age << endl;
	}
	return 0;
}

我的代码

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>

#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int

using namespace std;

struct Peo {
	string name;
	int fat,id,big;
	int age;
	bool is_new;
	Peo():age(-1), is_new(false){}
	bool operator<(const Peo& b) {
		if (age == b.age)
			return name < b.name;
		return age > b.age;
	}
}a[111];

void solve(int t) {
	int n,age,len = 0;
	cin >> n;
	map<string, int> mp;
	string s1, s2;
	for (int i = 0; i < n; i++) {
		cin >> s1 >> s2 >> age;
		if (!mp.count(s1)) 
			mp[s1] = len++;

		if (!mp.count(s2))
			mp[s2] = len++;

		int fid = mp[s1], sid = mp[s2];

		if (a[fid].is_new == false) {
			a[fid].name = s1;
			//cout << a[fid].name << endl;
			a[fid].id = fid;
			a[fid].is_new = true;
		}
		
		if (a[sid].is_new == false) {
			a[sid].name = s2;
			//cout << a[sid].name << endl;
			a[sid].id = sid;
			a[sid].is_new = true;
		}
		a[sid].big = age;
		a[sid].fat = fid;
	}

	int ted = mp["Ted"];
	a[ted].age = 100;
	queue<int> q;
	q.push(ted);
	while (!q.empty()) {
		int now = q.front();
		q.pop();

		for (int i = 0; i < len; i++) {
			if (a[i].fat == now && i!= ted) {
				a[i].age = a[now].age - a[i].big;
				q.push(i);
			}
		}
	}
	sort(a, a + n + 1);
	cout << "DATASET " << t << endl;
	for (int i = 1; i <= n; i++)
		cout << a[i].name << ' ' << a[i].age << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	for (int i = 1; i <= t; i++) 
		solve(i);
		
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值