codeforces D. Design Tutorial: Inverse the Problem

简介:

codeforces D. Design Tutorial: Inverse the Problem

题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树,
使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等!

思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径, 如果存在一棵树,那么
这个树中a->b的距离一定是这个图中所有a->b中路径长度最短的一条!所以我们根据边权,
建立一棵MST树!再将MST树中的任意两点之间的距离求出来,看是否和矩阵中的对应的节点
对距离相同!

复制代码
  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<vector>
  5 #include<algorithm>
  6 #define N 2005
  7 #define M 2000005
  8 using namespace std; 
  9 
 10 int mp[N][N];
 11 int mpp[N][N];
 12 int f[N];
 13 int vis[N];
 14 int n;
 15 
 16 struct node{
 17     int v, dist;
 18     node(){}
 19     node(int v, int dist){
 20         this->v = v;
 21         this->dist = dist;
 22     }
 23 };
 24 
 25 vector<node>tmp[N];
 26 
 27 struct edge{
 28     int x, y, d;
 29     edge(int x, int y, int d){
 30         this->x = x;
 31         this->y = y;
 32         this->d = d;
 33     }
 34     edge(){}
 35 };
 36 
 37 int cnt;
 38 edge e[M];
 39 
 40 bool cmp(edge a, edge b){
 41     return a.d < b.d;
 42 }
 43 
 44 int getFather(int x){
 45     return x == f[x] ? x : f[x] = getFather(f[x]);
 46 }
 47 
 48 bool _union(int x, int y){
 49     int fx = getFather(x), fy = getFather(y);
 50     if( fx != fy){
 51         f[fx] = fy;
 52         return true;
 53     }
 54     return false;
 55 }
 56 
 57 void dfs(int u, int cur, int dist){
 58     vis[u] = 1;
 59     int len = tmp[u].size();
 60     for(int i = 0; i<len; ++i){
 61         int v = tmp[u][i].v;
 62         if( !vis[v] ){
 63             mpp[cur][v] = mpp[v][cur] = dist+tmp[u][i].dist;
 64             dfs(v, cur, dist+tmp[u][i].dist);
 65         }
 66     }
 67 }
 68 
 69 int main(){
 70     scanf("%d", &n);
 71     bool flag = true;
 72     bool flag1 = false;    
 73     for(int i = 1; i <= n; ++i){
 74         f[i] = i;
 75         for(int j = 1; j <= n; ++j){
 76             scanf("%d", &mp[i][j]);
 77             if(j > i) e[cnt++] = edge(i, j, mp[i][j]);//将边存起来 
 78             if(i==j && mp[i][j] != 0) flag = false;//是否自身到自身有权值 
 79             if( i!=j && !mp[i][j]) flag1 = true;//是否都是全零 
 80         }
 81     }
 82     if(!flag1 && flag){
 83         sort(e, e+cnt, cmp);
 84         for(int i=0; i<cnt; ++i)
 85             if( _union(e[i].x, e[i].y) )
 86                 tmp[e[i].x].push_back(node(e[i].y, e[i].d)), tmp[e[i].y].push_back(node(e[i].x, e[i].d));
 87        
 88         for(int i=1; flag && i<n; ++i){//求最小生成树中任意两个节点的距离 
 89             memset(vis, 0, sizeof(vis));
 90             dfs(i, i, 0);
 91             for(int j=i+1; flag && j<=n; ++j)
 92                 if(!(mp[i][j] == mpp[i][j] && mp[i][j] == mp[j][i]))//如果最小生成树中的任意两点距离和给定的对应的两点之间的距离不相等 
 93                    flag = false;
 94         }
 95          
 96         if( flag ) printf("YES\n");
 97         else printf("NO\n");
 98     }
 99     else printf("NO\n");
100     return 0;
101 } 
复制代码









本文转自 小眼儿 博客园博客,原文链接:http://www.cnblogs.com/hujunzheng/p/4001152.html,如需转载请自行联系原作者
目录
相关文章
|
数据库
When Tech Meets Love – Smarter Ways to NOT be Single
It’s that time of year again. Single’s Day (a.k.a Double 11) is just around the corner, people buying gifts for loved ones.
1634 0
When Tech Meets Love – Smarter Ways to NOT be Single
|
机器学习/深度学习 算法 前端开发
文献翻译:Statistical Approaches for Gene Selection, Hub Gene Identification and Module Interaction in...
摘要 信息基因的选择是基因表达研究中的重要问题。基因表达数据的小样本量和大量基因特性使选择过程复杂化。此外,所选择的信息基因可以作为基因共表达网络分析的重要输入。
1182 0
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]PrI.6.1
Given a basis $U=(u_1,\cdots,u_n)$ not necessarily orthonormal, in $\scrH$, how would you compute the biorthogonal basis $\sex{v_1,\cdots,v_n}$? Find ...
680 0
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.9
(Schur's Theorem) If $A$ is positive, then $$\bex \per(A)\geq \det A. \eex$$   Solution. By Exercise I.
554 0
|
应用服务中间件 AHAS Perl
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.6
Let $A$ be a nilpotent operator. Show how to obtain, from aJordan basis for $A$, aJordan basis of $\wedge^2A$.
796 0
|
资源调度
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.5
Show that the inner product $$\bex \sef{x_1\vee \cdots \vee x_k,y_1\vee \cdots\vee y_k} \eex$$ is equal to the permanent of the $k\times k$ matrix $\sex{\sef{x_i,y_j}}$.
551 0
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.7
Prove that for any vectors $$\bex u_1,\cdots,u_k,\quad v_1,\cdots,v_k, \eex$$ we have $$\bex |\det(\sef{u_i,v_j})|^2 \leq \det\sex{\sef{u_i,u_j}}\cdot...
604 0
|
关系型数据库 Perl 机器学习/深度学习
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]Contents
I find it may cost me so much time in doing such solutions to exercises and problems....I am sorry that I could not be persistent in doing it.
1673 0