CF450D - Jzzhu and Cities (贪心,BFS)

题目

Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.

Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.Input

The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105).

Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109).

Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109).

It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.Output

Output a single integer representing the maximum number of the train routes which can be closed.

Examples

input

5 5 3
1 2 1
2 3 2
1 3 3
3 4 4
1 5 5
3 5
4 5
5 5
output

2
input

2 2 3
1 2 2
2 1 3
2 1
2 2
2 3
output

2

题意:输入一个n*m的矩阵,'.'表示该位置为空。'*'表示该位置非空。用1*2的瓷砖去填满空位置,如果只有1种填法则输出,否则输出Not unique

题解:遍历所有点,对于每个点统计它周围为'.'的个数,存入deg[i]

对于所有deg[i]=1的节点,放入队列

然后填入砖,填入以后更新deg[i],对于新的deg[i]=1入队,直到填完

最后check一遍,如果只有单一方法则输出

代码

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2010;
int n, m;
char s[2010][2010];
int deg[2010][2010];

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

struct node {
    int a, b;
    node(int a = 0, int b = 0) : a(a), b(b){};
};

queue<node> Q;

int count(int x, int y) {
    int cnt = 0;
    for (int i = 0; i < 4; i++) {
        int xx = x + dx[i], yy = y + dy[i];
        if (xx >= 0 && xx < n && yy >= 0 && yy < m && s[xx][yy] == '.') {
            cnt++;
        }
    }
    return cnt;
}

void update(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int xx = x + dx[i], yy = y + dy[i];
        if (xx >= 0 && xx < n && yy >= 0 && yy < m && s[xx][yy] == '.') {
            deg[xx][yy] = count(xx, yy);
            if (deg[xx][yy] == 1) {
                Q.push(node(xx, yy));
            }
        }
    }
}

void bfs() {
    memset(deg, 0, sizeof(deg));
    while (!Q.empty()) {
        Q.pop();
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (s[i][j] == '*') {
                continue;
            }
            deg[i][j] = count(i, j);
            if (deg[i][j] == 1) {
                Q.push(node(i, j));
            }
        }
    }
    int x, y;
    while (!Q.empty()) {
        node no = Q.front();
        Q.pop();
        x = no.a;
        y = no.b;
        for (int i = 0; i < 4; i++) {
            int xx = x + dx[i], yy = y + dy[i];
            if (xx >= 0 && xx < n && yy >= 0 && yy < m && s[xx][yy] == '.') {
                switch (i) {
                case 0:
                    s[x][y] = '<', s[xx][yy] = '>';
                    break;
                case 1:
                    s[xx][yy] = '<', s[x][y] = '>';
                    break;
                case 2:
                    s[x][y] = '^', s[xx][yy] = 'v';
                    break;
                case 3:
                    s[xx][yy] = '^', s[x][y] = 'v';
                    break;
                default:
                    break;
                }
                update(x, y);
                update(xx, yy);
                break;
            }
        }
    }
}

bool check() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (s[i][j] == '.')
                return false;
        }
    }
    return true;
}

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        scanf("%s", s[i]);
    }
    bfs();
    if (check()) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cout << s[i][j];
            }
            cout << endl;
        }
    } else {
        cout << "Not unique" << endl;
    }
    return 0;
}