2020牛客寒假算法基础集训营6-导航系统
好题
想法很复杂,但是很好实现。。。最小生成树+prim
#pragma GCC optimize(3,"Ofast","inline") //G++
#include<bits/stdc++.h>
#define TEST freopen("C:\\Users\\hp\\Desktop\\ACM\\in.txt","r",stdin);
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fcout cout<<setprecision(4)<<fixed
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
const int inf=0x3f3f3f3f;
const ll INF=0x7fffffffffffffff;
const int mod=1e9+7;
const int maxn = 1e6+5;
const double eps=1e-8;
template<typename T> void read(T &x){
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
read(first);
read(args...);
}
int sgn(double a){
return a<-eps?-1:a<eps?0:1;
}
struct node{
int u,v,val;
bool friend operator <(const node a,const node b){
return a.val<b.val;
}
}a[maxn];
int re[505];
int fin(int x){
return re[x]==x?x:re[x]=fin(re[x]);
}
int mp[505][505],G[505][505];
void solve(){
int n,cnt=0;
read(n);
for(int i=1;i<=n;i++){
re[i]=i;
for(int j=1;j<=n;j++){
read(mp[i][j]),a[++cnt]=node{i,j,mp[i][j]};
G[i][j]=i==j?0:inf;
}
}
vector<int>res;
sort(a+1,a+cnt+1);
for(int i=1;i<=cnt;i++){
int xx=fin(a[i].u),yy=fin(a[i].v);
if(xx!=yy){
G[a[i].u][a[i].v]=G[a[i].v][a[i].u]=a[i].val;
res.push_back(a[i].val);
re[xx]=yy;
}
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
G[i][j]=min(G[i][j],G[i][k]+G[k][j]);
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(G[i][j]!=mp[i][j]){
cout<<"No\n";
return ;
}
}
}
cout<<"Yes\n";
sort(res.begin(),res.end());
for(auto it:res){
cout<<it<<"\n";
}
}
int main()
{
solve();
}