[FJOI2018] Solomon's Treasure

Probably the last of a solution to a problem, in fact, just want to make a story about the decadence

According to ancient legend records, that is, on behalf of King Solomon's wisdom, but also a symbol of wealth. He established a strong and wealthy country, gathered a large number of ivory, gold and diamonds, and these priceless treasures hidden in a mysterious place, which is Jesus Christ's attention, "Solomon's treasure." For centuries, people have been looking for treasures of ancient civilizations these long lost, looking for treasure rich in gold and diamonds. Once the treasure of King Solomon pursue risk-takers who have never came back, no one has solved this puzzle. Henry baron in a lucky trip outside to get three hundred years ago, a Portuguese nobleman left written on scrolls of King Solomon's Mines map and a treasure hunt secrets. Lured by this treasure map, the Baron invited Colonel John Henry and brave elephant hunter boast Terman began the dangerous journey to find buried in the dark underground treasure of King Solomon. They crossed boundless deserts and luxuriant forests, surging through the whitewater rapids, climb the towering snow-capped mountains, desert heat suffered cold and snow, in the guidance of the treasure map of Africa down to a the original land of mystery Kukuanna. There brutal human sacrifice system has one thousand wives have a one-eyed tyrant Thwala, there are like vultures generally ugly and deceitful witch Jia Guer old and die, as well as beautiful, intelligent peerless beauty Fratta. In this piece of land three strange and perilous treasure hunt heroes have gone through hardships. Finally with the help of phoenix Fratta found a huge treasure cave of treasure these priceless treasures in the depths. However, in a carefully planned witch Jiagu Er, a disaster is quietly approaching.

Booty hole Portal Portal is very strong and closed, if you do not know the open secret portal is unable to open the treasure cave of the portal. In treasure portal side hole has a strange rectangular array password. The treasure Tips described, the left side of each row of the array and password top of each column has a ruby ​​button. Each button can be turned to the right or to the left. Each time the button 7 is rotated left, the respective row or column key numbers are incremented by one. Each time the button is rotated to the right, the respective row or column numbers are decreased by one. In the matrix array password several positions studded with emeralds. Only when all digital password Emerald position with identical records treasure map, closed portal will open. Witch Jiagu Er had learned the secret door. In order to prevent treasure hunters to open the portal, the password to open the door for the witch Jiagu Er array sets the initial state of all zeros. Portal trying to open the treasure hunters if they can not quickly rotate button to make all the numbers on the Emerald and described as a treasure map, it will start treasure hole mysterious organ, the treasure hunters were drowned attacked and killed.

You can help three treasure hunters successfully open the treasure cave of the portal do?

Programming tasks: password given array, to find the correct password button ruby ​​rotation sequence.

Input Format

The first line of input has a positive integer T (T≤5) expressed T set of data. The first line of each data has three positive integers n, m and k, n represents Portal password array consists of rows and m columns, 0 <n, m, k ≤ 1000. From top to bottom the rows are numbered 1,2, ......, n; columns from left to right are numbered 1,2, ......, m. The next k rows each row having three integers X, y, c, respectively, emerald k-th position in the password and the password in the array, x is the column number and y is the row number, c password at that location.

Output Format

For each test, with the output line Yes or No. Yes rotational output is obtained indicating the presence of the correct button sequence ruby ​​password. No output said it could not find the correct password rotation sequence ruby ​​buttons.

Sample input and output

Input # 1

2
2 2 4
1 1 0
1 2 0
2 1 2
2 2 2
2 2 4
1 1 0
1 2 0
2 1 2
2 2 1

Output # 1

Yes
No

Description / Tips

To 100% of the data,. 1 ≤ n-, m, ≤ 1000 K, K * ≤ n-m, | C | ≤ 1000000.

Solution

For each Emerald, the horizontal and vertical coordinates even his side, then the addition or subtraction for each point is the equivalent of the operation of all adjacent edges. For each edge has del [u] + del [v] == w

If not the above equation, it is not legitimate

Met, then it is updated del [v] and then thrown into the queue to go to bfs

Finally, determine what each point is not legitimate enough

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
inline long long read() {
  long long x = 0; int f = 0; char c = getchar();
  while (c < '0' || c > '9') f |= c == '-', c = getchar();
  while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return f ? -x : x;
}

int n, m, k, cnt, hd[2005], del[2005];
struct szh {
  int to, nxt, w;
}a[2005];
bool vis[2005];
queue<int> q;
inline void add(int x, int y, int z) {
  a[++cnt].to = y, a[cnt].w = z, a[cnt].nxt = hd[x], hd[x] = cnt;
}
inline bool bfs(int s) {
  while (!q.empty()) q.pop();
  q.push(s); vis[s] = 1;
  while (!q.empty()) {
    int u = q.front(); q.pop();
    for (int i = hd[u], v; v = a[i].to, i; i = a[i].nxt) 
      if (vis[v]) {
        if (del[u] + del[v] != a[i].w) return 0;
      }
      else {
        del[v] = a[i].w - del[u], vis[v] = 1;
        q.push(v); 
      }
  } 
  return 1;
}
int main() {
  int T = read();
  while (T--) {
    memset(vis, 0, sizeof vis);
    memset(hd, 0, sizeof hd);
    cnt = 0;
    n = read(); m = read(); k = read();
    for (int i = 1, x, y, z; i <= k; ++i) {
      x = read(); y = read(); z = read();
      add(x, y + n, z); add(y + n, x, z);
    }
    bool ok = 1;
    for (int i = 1; i <= n + m && ok; ++i)
      if (!vis[i]) ok &= bfs(i);
    if (ok) printf("Yes\n");
    else printf("No\n");
  }
  return 0;
}

Guess you like

Origin www.cnblogs.com/kylinbalck/p/11853476.html