Codeforces Round #270(利用prim算法)
2 seconds
256 megabytes
standard input
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).
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.
If there exists such a tree, output "YES", otherwise output "NO".
3
0 2 7
2 0 9
7 9 0
YES
3
1 2 7
2 0 9
7 9 0
NO
3
0 2 2
7 0 9
7 9 0
NO
3
0 1 1
1 0 1
1 1 0
NO
2
0 0
0 0
NO
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算法)的更多相关文章
- Codeforces Round #270 1003
		
Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...
 - Codeforces Round #270 1002
		
Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...
 - Codeforces Round #270 1001
		
Codeforces Round #270 1001 A. Design Tutorial: Learn from Math time limit per test 1 second memory l ...
 - Codeforces Round #270 A~D
		
Codeforces Round #270 A. Design Tutorial: Learn from Math time limit per test 1 second memory limit ...
 - Codeforces Round #270 D C B A
		
谈论最激烈的莫过于D题了! 看过的两种做法不得不ORZ,特别第二种,简直神一样!!!!! 1th:构造最小生成树. 我们提取所有的边出来按边排序,因为每次我们知道边的权值>0, 之后每次把边加入 ...
 - Codeforces Round #270
		
A 题意:给出一个数n,求满足a+b=n,且a+b均为合数的a,b 方法一:可以直接枚举i,n-i,判断a,n-i是否为合数 #include<iostream> #include< ...
 - codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)
		
题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...
 - 多种方法过Codeforces Round #270的A题(奇偶法、打表法和Miller_Rabin(这个方法才是重点))
		
题目链接:http://codeforces.com/contest/472/problem/A 题目: 题意:哥德巴赫猜想是:一个大于2的素数一定可以表示为两个素数的和.此题则是将其修改为:一个大于 ...
 - Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS
		
题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...
 
随机推荐
- Wix学习整理(4)——关于WiX文件格式和案例HelloWorld的分析
			
原文:Wix学习整理(4)--关于WiX文件格式和案例HelloWorld的分析 关于WiX文件格式 .wxs是WiX的源文件扩展名..wxs文件以类XML文件的格式来指定了要构造Windows In ...
 - POJ 2948 DP
			
一个row*col的矩阵,每一个格子内有两种矿yeyenum和bloggium,而且知道它们在每一个格子内的数量是多少.最北边有bloggium的收集站,最西边有 yeyenum 的收集站.如今要在这 ...
 - Android Studio 入门(转)
			
本文适用于从Eclipse转AndroidStudio的开发者 最近打算写一个系列的android初级开发教程,预计40篇以上的文章,结合我实际工作中的经验,写一些工作中经常用到的技术,让初学者可以少 ...
 - java的提取与替换操作
			
public class Demo02 { public static void main(String args[]){ String str = "java 技术学习班 2007032 ...
 - OpenCV 编程简单介绍(矩阵/图像/视频的基本读写操作)
			
PS. 因为csdn博客文章长度有限制,本文有部分内容被截掉了.在OpenCV中文站点的wiki上有可读性更好.而且是完整的版本号,欢迎浏览. OpenCV Wiki :<OpenCV 编程简单 ...
 - iOS UITableView的Section Footer加入button
			
郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠.支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 在处理UI ...
 - 【JavaEE基础】在Java中如何使用jdbc连接Sql2008数据库
			
我们在javaEE的开发中,肯定是要用到数据库的,那么在javaEE的开发中,是如何使用代码实现和SQL2008的连接的呢?在这一篇文章中,我将讲解如何最简单的使用jdbc进行SQL2008的数据库的 ...
 - Android编程获取网络连接状态及调用网络配置界面
			
获取网络连接状态 随着3G和Wifi的推广,越来越多的Android应用程序需要调用网络资源,检测网络连接状态也就成为网络应用程序所必备的功能. Android平台提供了ConnectivityMan ...
 - Leet code —Jump Game
			
问题叙述性说明: Given an array of non-negative integers, you are initially positioned at the first index of ...
 - ZOJ 2334(Monkey King-左偏树第一题)
			
Monkey King Time Limit: 10 Seconds Memory Limit: 32768 KB Once in a forest, there lived N aggre ...