D. Design Tutorial: Inverse the Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder
Open 2014 Round 2C, InverseRMQ, is a good example.

Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance
matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).

Input

The first line contains an integer n (1 ≤ n ≤ 2000)
— the number of nodes in that graph.

Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109)
— the distance between node i and node j.

Output

If there exists such a tree, output "YES", otherwise output "NO".

Sample test(s)
input
3
0 2 7
2 0 9
7 9 0
output
YES
input
3
1 2 7
2 0 9
7 9 0
output
NO
input
3
0 2 2
7 0 9
7 9 0
output
NO
input
3
0 1 1
1 0 1
1 1 0
output
NO
input
2
0 0
0 0
output
NO
Note

In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7.

In the second example, it is impossible because d1, 1 should
be 0, but it is 1.

In the third example, it is impossible because d1, 2 should
equal d2, 1.

给定一个矩阵,表示每两个节点之间的权值距离,问能否够相应生成一棵树,

使得这棵树中的随意两点之间的距离和矩阵中的相应两点的距离相等。

思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径, 假设存在一棵树。那么

这个树中a->b的距离一定是这个图中全部a->b中路径长度最短的一条!

所以我们依据边权,

建立一棵MST树!

再将MST树中的随意两点之间的距离求出来,看是否和矩阵中的相应的节点

对距离同样,我们首先构造一个最小生成树,然后比較各各点之间的距离是否与题目给出的距离相等,能够用dfs搜索整张图的每两个点之间的距离.以下给的做法非dfs做的,用一个数组f[][],记录x,y两点之间的距离,算距离的时候是通过眼下点的前驱找,也就是说须要一个数组记录前驱,这样就能够不用dfs了,直接能够算.看完代码就明确了.....


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2010;
const int INF = 0x3f3f3f3f;
int graph[maxn][maxn];
int prior[maxn];
int visit[maxn];
int dis[maxn];
int f[maxn][maxn];
int n;
bool check()
{
for(int i = 0; i < n; i++)
{
dis[i] = INF;
if(graph[i][i] != 0) return false;
for(int j = i+1 ; j < n; j++)
{
if(graph[i][j] != graph[j][i] || graph[i][j] == 0) return false;
}
} memset(visit,0,sizeof(visit));
memset(prior,-1,sizeof(prior));
memset(f,0,sizeof(f));
int cent = n;
dis[0]=0;
while(cent--)//循环n次是由于要初始化
{ int min = -1;
for(int i = 0; i < n; i++)
{
if(!visit[i] && (min == -1 || dis[i] < dis[min]))
{
min = i;
}
}
for(int i = 0; i < n; i++)//在prim算法里面添加这层循环里面的内容算距离
{
if(visit[i])//必须是已经訪问过的点,才干算距离
{
f[i][min] = f[min][i] = f[i][prior[min]] + dis[min];
}
}
visit[min] = true;
for(int i = 0; i < n; i++)
{
if(dis[i] > graph[min][i] )
{
dis[i] = graph[min][i];
prior[i] = min;//记录前驱
}
}
} for(int i = 0; i < n; i++)
{
for(int j = 0 ; j < n; j++)
{
if(f[i][j] != graph[i][j])
{
return false;
}
}
}
return true;
}
int main()
{
#ifdef xxz
freopen("in","r",stdin);
#endif // xxz
while(cin>>n)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
cin>>graph[i][j];
} if(check()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Codeforces Round #270(利用prim算法)的更多相关文章

  1. Codeforces Round #270 1003

    Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...

  2. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...

  3. Codeforces Round #270 1001

    Codeforces Round #270 1001 A. Design Tutorial: Learn from Math time limit per test 1 second memory l ...

  4. Codeforces Round #270 A~D

    Codeforces Round #270 A. Design Tutorial: Learn from Math time limit per test 1 second memory limit ...

  5. Codeforces Round #270 D C B A

    谈论最激烈的莫过于D题了! 看过的两种做法不得不ORZ,特别第二种,简直神一样!!!!! 1th:构造最小生成树. 我们提取所有的边出来按边排序,因为每次我们知道边的权值>0, 之后每次把边加入 ...

  6. Codeforces Round #270

    A 题意:给出一个数n,求满足a+b=n,且a+b均为合数的a,b 方法一:可以直接枚举i,n-i,判断a,n-i是否为合数 #include<iostream> #include< ...

  7. codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)

    题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...

  8. 多种方法过Codeforces Round #270的A题(奇偶法、打表法和Miller_Rabin(这个方法才是重点))

    题目链接:http://codeforces.com/contest/472/problem/A 题目: 题意:哥德巴赫猜想是:一个大于2的素数一定可以表示为两个素数的和.此题则是将其修改为:一个大于 ...

  9. Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS

    题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...

随机推荐

  1. HDU1789Doing Homework again(贪婪)

    HDU1789Doing Homework again(贪心) 题目链接 题目大意:给你n们作业的最后期限和过了这个期限没做须要扣的分数.问如何安排能够使得扣分最少. 解题思路:贪心,将扣分多的作业排 ...

  2. 10招让你成为杰出的Java程序员(转)

    如果你是一个热衷于技术的 Java 程序员, 那么下面的 10 个要点可以让你在众多 Java 开发人员中脱颖而出. 1. 拥有扎实的基础和深刻理解 OO 原则 对于 Java 程序员,深刻理解 Ob ...

  3. QT怎样在QTableWidge显示图片

      <span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">QTableWi ...

  4. F5 root密码恢复

    使用串口线缆链接F5的串口和PC相连接,调节串口的波特率为12000,重启F5 后在启动菜单上和linux 单用户模式一样操作即可.

  5. The method getDispatcherType() is undefined for the type HttpServletRequest 升级到tomcat8(转)

    配置项目,从tomcat低版本,放到tomcat8时,正常的项目居然报错了: The method getDispatcherType() is undefined for the type Http ...

  6. 首次启动优美新手指引tip

    在开发商业应用时候,用户第一次进入app,有种无从下手的感觉,我们作为开发人员要提供可用户一些指引, 这些指引不能让用户看着唐突,要舒服的展示给用户,带着用户愉快的使用我们的app. 怎么让用户舒服呢 ...

  7. cocos2d-x快乐的做让人快乐的游戏3:cocos-2d 3.x中的物理世界

    Cocos2d-x 3.0+ 中全新的封装的物理引擎给了开发人员最大的便捷,你不用再繁琐与各种物理引擎的细节,全然的封装让开发人员能够更快更好的将物理引擎的机制加入�到自己的游戏中,简化的设计是从2. ...

  8. c++对象指针-01(转载)

    1.指向对像的指针在建立对像时,编译系统会为每一个对像分配一定的存储空间,以存放其成员,对像空间的起始地址就是对像的指针.可以定义一个指针变量,用来存和对像的指针.如果有一个类:class Time{ ...

  9. 【JavaEE基础】在Java中如何使用jdbc连接Sql2008数据库

    我们在javaEE的开发中,肯定是要用到数据库的,那么在javaEE的开发中,是如何使用代码实现和SQL2008的连接的呢?在这一篇文章中,我将讲解如何最简单的使用jdbc进行SQL2008的数据库的 ...

  10. Radmin远程控制软件

    Radmin远程控制软件 日期:2015-08-20     作者:lujl   Radmin是一款快速的远程控制软件,可以用来远程管理公司或个人计算机来实现远程办公.你可以通过鼠标和键盘来控制远程的 ...