@topcoder - 2017TCOAlgorithmRound2A - D1L2@ DistanceZeroAndOne


@description@

None of the n points to FIG simple communication, numbered from 0 to n-1.

We are now given distance of each point 0. dist0 [], the distance of each point dist1 1 [], the entire FIG reduction, or no solution is determined.

The Constraints
n-between 2-50.
dist0 dist1 in the element are between 0 to n-1.

Examples
0)
{0,2,1}
{2,0,1}
Returns: {
"NNY",
"NNY",
"YYN"}
entire graph 0--2--1.

1)
{0,2,1}
{1,0,2}
Returns: { }
dist0[1] ≠ dist1[0]。

@solution@

The triangle inequality, if there is an edge between u and v, then | dist0 [u] - dist0 [v] | ≤ 1 and | dist1 [u] - dist1 [v] | ≤ 1.

If edges can be connected between the u, v (i.e., satisfies the triangle inequality), then even the (u, v).
Obviously edge even more, the more accurate the distance between points.
So if a solution, the above scheme will be able to get even side a valid solution.

We even run again after the two sides finished edge bfs test it meets this figure with dist1 the limit dist0.

@accepted code@

#include<queue>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
class DistanceZeroAndOne{
    #define MAXN 50
    private:
    int a[MAXN][MAXN], n;
    int abs(int x) {return x >= 0 ? x : -x;}
    int d[MAXN];
    public:
    void bfs(int x) {
        for(int i=0;i<n;i++)
            d[i] = n;
        d[x] = 0; queue<int>que; que.push(x);
        while( !que.empty() ) {
            int f = que.front(); que.pop();
            for(int i=0;i<n;i++)
                if( a[f][i] && d[f] + 1 < d[i] ) {
                    d[i] = d[f] + 1, que.push(i);
                }
        }
    }
    vector<string>ans;
    vector<string>construct(vector<int>d0, vector<int>d1) {
        ans.clear(), n = d0.size();
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if( i != j && abs(d0[i] - d0[j]) <= 1 && abs(d1[i] - d1[j]) <= 1 )
                    a[i][j] = 1;
        bool flag = true;
        bfs(0);
        for(int i=0;i<n;i++)
            if( d[i] != d0[i] )
                flag = false;
        if( !flag ) return ans;
        bfs(1);
        for(int i=0;i<n;i++)
            if( d[i] != d1[i] )
                flag = false;
        if( !flag ) return ans;
        for(int i=0;i<n;i++) {
            string s = "";
            for(int j=0;j<n;j++)
                if( a[i][j] ) s = s + 'Y';
                else s = s + 'N';
            ans.push_back(s);
        }
        return ans;
    }
};

@details@

like. . . Also no details. . .

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11568373.html