输入样例:
6
F 1 4:1
F 2 1:3 4:10
F 2 4:2 2:2
M 2 5:1 3:2
M 2 2:2 6:2
M 2 3:1 2:5
输入样例:
2 3
4
思路
弗洛伊德暴力求解
注意可以会有一行全是
i
n
f
inf
inf的存在,也就是一个人和其他任意一人都没有联系。
代码
#include<bits/stdc++.h>
#include<algorithm>
#define inf 1e9
#define ll long long
#define pii pair<int,int>
using namespace std;
const int N = 505;
int n, m, t, k, s;
int dis[N][N];
int sex[N];
map<int,int>p;
vector<int>female;
vector<int>male;
void init(int l){
for(int i = 1;i <= l;i++){
for(int j = 1;j <= l;j++)
{
if(i==j)dis[i][j] = 0;
else dis[i][j] = inf;
}
}
}
bool cmp(int x, int y){
if(p[x]==p[y])return x < y;
return p[x] < p[y];
}
void solve(){
cin>>n;init(n);
for(int i = 1;i <= n;i++){
char ch;
int no, d;
cin>>ch>>k;
sex[i] = ch=='F'?1:0;
for(int j = 0;j < k;j++){
cin>>no>>ch>>d;
dis[i][no] = d;
}
}
for(int k = 1;k <= n;k++){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(dis[i][j] > dis[i][k] + dis[k][j]){
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
}
for(int i = 1;i <= n;i++){
int d = -1;
for(int j = 1;j <= n;j++){
if(sex[i]!=sex[j]){
d = max(d,dis[j][i]);
}
}
if(d!=-1){
p[i] = d;
if(sex[i])female.push_back(i);
else male.push_back(i);
}
}
sort(female.begin(),female.end(),cmp);
sort(male.begin(),male.end(),cmp);
for(int i = 0;i < female.size();i++){
if(p[female[i]]==p[female[0]]){
if(i==0)cout<<female[i];
else cout<<" "<<female[i];
}
else break;
}cout<<"\n";
for(int i = 0;i < male.size();i++){
if(p[male[i]]==p[male[0]]){
if(i==0)cout<<male[i];
else cout<<" "<<male[i];
}
else break;
}
}
int main(){
solve();
return 0;
}
题后感
比赛心态炸裂,该题也是赛后补题完成的。