Codeforces 472D Design Tutorial: Inverse the Problem【MST+SPFA+思维】

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".

Examples
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.


题目大意:


给你一个N*N的邻接矩阵,问你是否满足原图是一棵树。


思路:


1、问我们原图是否满足是一棵树,我们不妨将N*N的图跑出MST.并且标记哪些边是最小生成树中的边,然后重建出一个新的图。


2、对于我们重建出的这个图,如果我们两点间最短路处理出来和这个N*N的矩阵是一样的,那么结果就是YES.否则就是NO.

注意数据范围比较大,中间过程可能会出现爆INT的情况,所以我门初始的时候就写成LL比较稳妥一点。



Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
#define INF 1000000000000000000
struct node
{
    ll x,y,w,num;
}e[2005*2005];
struct node2
{
    ll from;
    ll to;
    ll w;
    ll next;
}ee[2005*3];
ll vis[20005];
ll head[200005];
ll a[2002][2002];
ll dist[2002][2002];
ll f[20005];
ll n;
ll cnt,cont;
ll cmp(node a,node b)
{
    return a.w<b.w;
}
ll find(ll a)
{
    ll r=a;
    while(f[r]!=r)
    r=f[r];
    ll i=a;
    ll j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
ll merge(ll a,ll b)
{
    ll A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    {
        f[B]=A;
    }
}
void add(ll from,ll to,ll w)
{
    ee[cont].to=to;
    ee[cont].w=w;
    ee[cont].next=head[from];
    head[from]=cont++;
}
void Get_map()
{
    cont=0;
    memset(head,-1,sizeof(head));
    for(ll i=1;i<=n;i++)f[i]=i;
    sort(e,e+cnt,cmp);
    for(ll i=0;i<cnt;i++)
    {
        if(find(e[i].x)!=find(e[i].y))
        {
            merge(e[i].x,e[i].y);
            add(e[i].x,e[i].y,e[i].w);
            add(e[i].y,e[i].x,e[i].w);
        }
    }
}
void SPFA(ll ss)
{
    memset(vis,0,sizeof(vis));
    for(ll i=1;i<=n;i++)dist[ss][i]=INF;
    vis[ss]=1;
    dist[ss][ss]=0;
    queue<ll >s;
    s.push(ss);
    while(!s.empty())
    {
        ll u=s.front();
        vis[u]=0;
        s.pop();
        for(ll i=head[u];i!=-1;i=ee[i].next)
        {
            ll v=ee[i].to;
            ll w=ee[i].w;
            if(dist[ss][v]>dist[ss][u]+w)
            {
                dist[ss][v]=dist[ss][u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%I64d",&n))
    {
        cnt=0;
        for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=n;j++)
            {
                scanf("%I64d",&a[i][j]);
                if(j>i&&a[i][j]!=0)
                {
                    e[cnt].num=(i-1)*n+j;
                    e[cnt].x=i;
                    e[cnt].y=j;
                    e[cnt++].w=a[i][j];
                }
            }
        }
        int flag=0;
        Get_map();
        for(ll i=1;i<=n;i++)SPFA(i);
        for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=n;j++)
            {
                if(dist[i][j]!=a[i][j])flag=1;
            }
        }
        if(flag==0)printf("YES\n");
        else printf("NO\n");
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值