Royal Decree
InterviewTime limit2sMemory limit512 MB
Given a friendship graph and bound d, find the largest possible difference between richest and poorest that satisfies the friend-difference constraint, or -1 if unbounded.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, BFS
- Solved
- No attempts yet
Problem
A kingdom has N people. The amount of money each person holds is a non-negative integer, and the people are numbered 1 through N.
One day the king proclaimed a decree. The money any person holds may differ from the money each of that person's friends holds by at most . In other words, for a person to hold , none of that person's friends may hold less than and none may hold more than .
Among all distributions that obey the decree, the people want the one where the gap between the richest person and the poorest person is as large as possible.
Given the number of people and the friendships, write a program that computes the largest possible gap.
Input
The first line contains the number of people ().
The second line contains ().
Each of the next lines describes the friendships. If the -th character of the -th line is Y, person and person are friends; if it is N, they are not. The -th character of the -th line is always N, and the -th character of the -th line equals the -th character of the -th line.
Output
Print on the first line the largest gap between the richest person and the poorest person over all distributions that obey the decree. If this gap is unbounded, print -1.
Explanation
Suppose there are only three people, person 1 is a friend of person 2, person 2 is a friend of person 3, and is 10. Giving person 1 100, person 2 110, and person 3 120 obeys the decree, so the gap reaches 20. If nobody is anybody's friend, no constraint applies at all and the gap is unbounded.