Codeforces Round #270 (utilizing prim algorithm)

Source: Internet
Author: User

D. Design Tutorial:inverse the problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard Inputou Tputstandard output

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

Now let's create a task this to. We'll use the task:you is given a tree, please calculate the distance between any pair of its nodes. Yes, it's very easy and the inverse version is a bit harder:you was given an n? x? 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 the that graph.

Then nextNLines each containsNIntegers di,? J (0?≤? D i,? J? ≤?109 )-the Distance between nodeIand nodeJ.

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's impossible because d1,?1 should was 0, but it was 1.

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

This question gives two points distance, ask you can not be composed of these points a tree, we first construct a minimum spanning tree, and then compare the distance between each point is equal to the distance from the topic, you can use DFS to search the entire graph of the distance between every two points. The procedure given below is not DFS made, with an array of f[] [], recording the distance between x, y two points, the distance is through the current point of the precursor to find, that is, the need for an array record precursor, so that you can not use DFS, directly can be counted. Read the code and see ...
#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];i        NT 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--)//Loop n times is due to initialize {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++)//Add the contents of this loop inside the prim algorithm to the distance {if (Visit[i])//must be an already visited point, in order to calculate the distance    From            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] = Grap                H[min][i]; Prior[i] = min;//record Precursor}}} 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 (utilizing prim algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.