博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #270(利用prim算法)
阅读量:4649 次
发布时间:2019-06-09

本文共 3854 字,大约阅读时间需要 12 分钟。

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
30 2 72 0 97 9 0
output
YES
input
31 2 72 0 97 9 0
output
NO
input
30 2 27 0 97 9 0
output
NO
input
30 1 11 0 11 1 0
output
NO
input
20 00 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 
#include
#include
#include
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"<

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

转载于:https://www.cnblogs.com/mfrbuaa/p/4752178.html

你可能感兴趣的文章
AVL树、splay树(伸展树)和红黑树比较
查看>>
多媒体音量条显示异常跳动
查看>>
运算符及题目(2017.1.8)
查看>>
React接入Sentry.js
查看>>
ssh自动分发密匙脚本样板
查看>>
转 小辉_Ray CORS(跨域资源共享)
查看>>
Linux安装postgresql
查看>>
MyBatis启动:MapperStatement创建
查看>>
【 全干货 】5 分钟带你看懂 Docker !
查看>>
[转]优化Flash性能
查看>>
popStar手机游戏机机对战程序
查看>>
Java Web项目结构
查看>>
lambda表达式树
查看>>
二次注入原理及防御
查看>>
会话记住已登录功能
查看>>
Linux内核分析——可执行程序的装载
查看>>
儿子和女儿——解释器和编译器的区别与联系
查看>>
第一阶段冲刺3
查看>>
父类引用指向子类对象
查看>>
网页如何实现下载功能
查看>>