列表

详情


NC212622. Jiufeng'sFootballTeam

描述

Jiufeng likes playing table football (shown as the following picture), but he does not know how to play football, so he plans to go to the playground.

Now there are N people(numbered from 1 to N) on the playground preparing for the game. Because they do not know each other and do not have enough experience, so they are easy to make mistakes and difficult to cooperate with. Now Jiufeng knows that there are some gaps between players which cause them making mistakes. As long as training for t minutes in the same team, they will not make mistakes any more. If two players have no gap between them, they will have no chances to make mistakes. The game can only be played when all players in the same teams do not have chances to make mistakes, and there are no more than two teams in total.

Now given all the information about all the participants, Jiufeng wants to know how to allocate players to minimize the team's training time. Please tell Jiufeng how long it takes.

Please note that the number of people can be different between the two teams.

输入描述

There are multiple test cases, the first line contains a single integer T (1 ≤ T ≤ 3) — the number of test cases.

The first line of each test case contains two integers N and M (1 ≤ N ≤ 2 × , 1 ≤ M ≤ ), indicating the number of people and connections.

Then followed M lines, each line contains three integers a_i, b_i(1 ≤ a_i, b_i ≤ N, a_i != b_i) and t_i(1 ≤ t_i), indicating that player a_i and player b_i need t_i minutes of training.

输出描述

For each test case, print the shortest training time for the two teams, also means the shortest waiting time before the start of the game.

示例1

输入:

1
4 6
1 4 50
2 3 100
1 2 1000
1 3 300
2 4 30
3 4 800

输出:

100

说明:

For the first sample, Jiufeng can arrange No. 1 and No. 4 in a team, and No. 2 and No. 3 in a team. It only takes 100 minutes for him to train. Here shows the picture:

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 266ms, 内存消耗: 1656K, 提交时间: 2020-09-27 13:18:51

#include<bits/stdc++.h>
using namespace std;
class lu
{
	public:
		int a;
		int b;
		int jl;
};
lu sz[100005];
int f[200005];
bool cmp(lu a,lu b)
{
	return a.jl>b.jl;
}
int getf(int a)
{
	if(f[a]==a)return a;
	f[a]=getf(f[a]);
	return f[a];
}
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		int a,b;
		cin>>a>>b;
		for(int i=1;i<=a*2+1;i++)
		{
			f[i]=i;
		}
		for(int i=1;i<=b;i++)
		{
			cin>>sz[i].a>>sz[i].b>>sz[i].jl;
		}
		sort(sz+1,sz+b+1,cmp);
		int ans=0;
		for(int i=1;i<=b;i++)
		{
			int n=getf(sz[i].a);
			int m=getf(sz[i].b);
			if(n==m)
			{
				ans=sz[i].jl;
				break;
			}
			else
			{
				f[n]=getf(sz[i].b+a);
				f[m]=getf(sz[i].a+a);
			}
		}
		cout<<ans<<endl;
	}
}

C++11(clang++ 3.9) 解法, 执行用时: 100ms, 内存消耗: 1704K, 提交时间: 2020-09-26 19:19:22

#include <bits/stdc++.h>
using namespace std;
#define maxn 100005
#define Maxn 20005

int n,m;
int w[Maxn*2];

struct Info{
	int a,b,hp;
	bool operator < (const Info &s) const{
		return hp>s.hp;
	}
}s[maxn];

int find(int a){
	return w[a]==a?a:w[a]=find(w[a]);
}	

int main(){
    int t; scanf("%d", &t); while(t--){
	for(int i=0;i<2*Maxn;i++)
		w[i]=i;
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++)
		scanf("%d%d%d",&s[i].a,&s[i].b,&s[i].hp);
	sort(s,s+m);
        int re=0;
	for(int i=0;i<m;i++){
		int a=find(s[i].a),b=find(s[i].b);
		if(a==b){
            re=s[i].hp;
			break;
		}
		w[a]=find(s[i].b+n);
		w[b]=find(s[i].a+n);
	}
	printf("%d\n", re);
    }
	
	return 0;
}

上一题