[USACO17FEB]Why Did the Cow Cross the Road III S

题目描述

Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of roads, making it impossible for his cows to travel around without crossing many of them.

为什么牛过马路? 其中一个简单的原因就是农民约翰的农场有很多道路,使得他的母牛不得不穿越许多道路。

FJ's farm is arranged as an N×NN \times NN×N square grid of fields ( 2≤N≤1002 \leq N \leq 1002N100 ), Certain pairs of adjacent fields (e.g., north-south or east-west) are separated by roads, and a tall fence runs around the external perimeter of the entire grid, preventing cows from leaving the farm. Cows can move freely from any field to any other adjacent field (north, east, south, or west), although they prefer not to cross roads unless absolutely necessary.

FJ的农场在 N×NN\times NN×N 的网格中( 2≤N≤1002\le N\le 1002N100 ),某些相邻的区域(例如,南北或东西)由道路分隔,高大的围栏围绕着整个格栅的外围,防止牛离开农场。 牛可以从任何场地自由移动到任何其他相邻的区域(北,东,南或西),不过除非不得已,她们并不愿意穿越道路。

There are KKK cows ( 1≤K≤100,K≤N21 \leq K \leq 100, K \leq N^21K100,KN2 ) on FJ's farm, each located in a different field. A pair of cows is said to be "distant" if, in order for one cow to visit the other, it is necessary to cross at least one road. Please help FJ count the number of distant pairs of cows.

在FJ的农场有 KKK 头牛( 1≤K≤100,K≤N21\le K\le 100,K\le N^{2}1K100,KN2 ),每个位于不同的区域。 定义一对牛是“遥远的”,是指让一头牛访问另一头牛时,必须至少穿过一条路。 请帮助FJ计算有多少对牛是“遥远的”。

输入输出格式

输入格式:

The first line of input contains NNN , KKK , and RRR . The next RRR lines describe RRR roads that exist between pairs of adjacent fields. Each line is of the form rrr ccc r′r'rc′c'c′ (integers in the range 1…N1 \ldots N1N ), indicating a road between the field in (row rrr , column ccc ) and the adjacent field in (row r′r'r′ , column c′c'c′ ). The final KKK lines indicate the locations of the KKK cows, each specified in terms of a row and column.

第一行输入包含 NNN , KKK 和 RRR 。 接下来的 RRR 行描述存在于相邻区域对之间的 RRR 条路。 每行的格式为 rrr ; ccc ; r′r'r′ ; c′c'c′ (都是在 1...N1...N1...N 中的整数),表示在两个相邻的区域(第 rrr 行第 ccc 列,和第 $r​'$ ​​ 行第 $c​'$ ​​ 列)之间的路。 最终的 KKK 行表示 KKK 头牛的位置,也用行列来表示。

输出格式:

Print the number of pairs of cows that are distant.

输出遥远的牛数量对。

输入输出样例

输入样例#1: 
3 3 3
2 2 2 3
3 3 3 2
3 3 2 3
3 3
2 2
2 3
输出样例#1: 
2

无脑爆搜;
首先我想到的是对于每一个牛都bfs一遍;
可想而知T的很惨
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;

inline int read(){int res=0;bool flag=0;char ch=getchar();while(!isdigit(ch)){if(res=='-')flag=1;ch=getchar();}while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch-'0');ch=getchar();}return flag?-res:res;}

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

int n, k, r;

bool can[110][110][5];

int mp[110][110];
int cowx[110], cowy[110];

int ans[110][110];
bool vis[110][110];

struct date
{
    int x;
    int y;
};
inline void bfs(int sx, int sy)
{
    queue <date> q;
    while (!q.empty()) q.pop();
    q.push((date){sx, sy});
    
    while (!q.empty())
    {
        int x = q.front().x, y = q.front().y;
        q.pop();
        vis[x][y] = 1;
        for (register int i = 1 ; i <= 4 ; i ++)
        {
            if (can[x][y][i]) continue;
            int tx = x + dx[i], ty = y + dy[i];
            if (tx <= 0 or tx > n or ty <= 0 or ty > n) continue;
            if (vis[tx][ty]) continue;
            q.push((date){tx, ty});
        }
    }
    
}

int main()
{
    n = read(), k = read(), r = read();
    for (register int i = 1 ; i <= r ; i++)
    {
        int a = read(), b = read(), x = read(), y = read();
        if (a == x)
        {
            if (b == y + 1) 
            {
                can[a][b][3] = 1;
                can[x][y][4] = 1;
            }
            else
            {
                can[a][b][4] = 1;
                can[x][y][3] = 1;
            }
        }
        else
        {
            if (a == x + 1)
            {
                can[a][b][1] = 1;
                can[x][y][2] = 1;
            }
            else
            {
                can[a][b][2] = 1;
                can[x][y][1] = 1;
            }
        }
    }
    
    for (register int i = 1 ; i <= k ; i ++)
    {
        int x = read(), y = read();
        mp[x][y] = 3;
        cowx[i] = x, cowy[i] = y;
    }
    
    for (register int i = 1 ; i <= k ; i ++)
    {
        memset(vis, 0, sizeof vis);
        bfs(cowx[i], cowy[i]);
        for (register int j = 1 ; j <= k ; j ++)
        {
            if (i == j) continue;
            if (vis[cowx[j]][cowy[j]] == 0)
            {
                ans[i][j] = 1;
            }
        }
    }
    
    int res = 0;
    for (register int i = 1 ; i <= k ; i ++)
    {
        for (register int j = 1 ; j <= i ; j ++)
        {
            if (ans[i][j]) res++;
        }
    }
    cout << res << endl;
    return 0;
}
zZhBr

然后又想我们可以搜出图中所有的联通块, 然后直接暴力相加;great√!

可是现实却是这样


残酷现实;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;

inline char nc()
{
    static const int BS = 1 << 22;
    static unsigned char buf[BS],*st,*ed;
    if(st == ed) ed = buf + fread(st=buf,1,BS,stdin);
    return st == ed ? EOF : *st++;
}

inline int read(){int res=0;bool flag=0;char ch=nc();while(!isdigit(ch)){if(res=='-')flag=1;ch=nc();}while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch-'0');ch=nc();}return flag?-res:res;}

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

int n, k, r;

bool can[110][110][5];
int mp[110][110];
int color[110][110];
int col;
int cowx[110], cowy[110];

struct date
{
    int x;
    int y;
};
inline void bfs(int sx, int sy)
{
    queue <date> q;
    while (!q.empty()) q.pop();
    q.push((date){sx, sy});
    
    while (!q.empty())
    {
        int x = q.front().x, y = q.front().y;
        q.pop();
        color[x][y] = col;
        for (register int i = 1 ; i <= 4 ; i ++)
        {
            if (can[x][y][i]) continue;
            int tx = x + dx[i], ty = y + dy[i];
            if (tx <= 0 or tx > n or ty <= 0 or ty > n) continue;
            if (color[tx][ty]) continue;
            q.push((date){tx, ty});
        }
    }
    
}

int main()
{
    n = read(), k = read(), r = read();
    for (register int i = 1 ; i <= r ; i++)
    {
        int a = read(), b = read(), x = read(), y = read();
        if (a == x)
        {
            if (b == y + 1) 
            {
                can[a][b][3] = 1;
                can[x][y][4] = 1;
            }
            else
            {
                can[a][b][4] = 1;
                can[x][y][3] = 1;
            }
        }
        else
        {
            if (a == x + 1)
            {
                can[a][b][1] = 1;
                can[x][y][2] = 1;
            }
            else
            {
                can[a][b][2] = 1;
                can[x][y][1] = 1;
            }
        }
    }
    
    for (register int i = 1 ; i <= k ; i ++)
    {
        int x = read(), y = read();
        mp[x][y] = 3;
        cowx[i] = x, cowy[i] = y;
    }
    
    for (register int i = 1 ; i <= k ; i ++)
    {
        if (!color[cowx[i]][cowy[i]])
        {
            col++;
            bfs(cowx[i], cowy[i]);
        }
    }
    int res = 0 ;    
    for (register int i = 1 ; i <= k ; i ++)
    {
        for (register int j = i + 1 ; j <= k ; j ++)
        {
            if (color[cowx[i]][cowy[i]] != color[cowx[j]][cowy[j]]) res++;
        }
    }
    printf("%d\n", res);
    return 0;
}
zZhBr
咳咳!stl队列常数巨大!
所以又手写队列;
WA!
what?
查了一年...
woc
我染色的时候顺序写错了!

真正代码:
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <queue>
  5 #include <cstring>
  6 using namespace std;
  7 
  8 #define nc getchar 
  9 inline int read(){int res=0;bool flag=0;char ch=nc();while(!isdigit(ch)){if(res=='-')flag=1;ch=nc();}while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch-'0');ch=nc();}return flag?-res:res;}
 10 
 11 int dx[] = {0, -1, 1, 0, 0}, dy[] = {0, 0, 0, -1, 1};
 12 
 13 int n, k, r;
 14 
 15 bool can[110][110][5];
 16 int color[110][110];
 17 int col;
 18 int cowx[110], cowy[110];
 19 
 20 struct date
 21 {
 22     int x;
 23     int y;
 24 }q[110*110];
 25 inline void bfs(int sx, int sy)
 26 {
 27     int l = 1, r = 1;
 28     q[l] = (date){sx, sy};
 29     color[sx][sy] = col;
 30     
 31     while (l <= r)
 32     {
 33         int x = q[l].x, y = q[l++].y;
 34         for (register int i = 1 ; i <= 4 ; i ++)
 35         {
 36             if (can[x][y][i]) continue;
 37             int tx = x + dx[i], ty = y + dy[i];
 38             if (tx <= 0 or tx > n or ty <= 0 or ty > n) continue;
 39             if (color[tx][ty]) continue;
 40             color[tx][ty] = col;
 41             q[++r] = (date){tx, ty};
 42         }
 43     }
 44 }
 45 
 46 int main()
 47 {
 48     n = read(), k = read(), r = read();
 49     for (register int i = 1 ; i <= r ; i++)
 50     {
 51         int a = read(), b = read(), x = read(), y = read();
 52         if (a == x)
 53         {
 54             if (b == y + 1) 
 55             {
 56                 can[a][b][3] = 1;
 57                 can[x][y][4] = 1;
 58             }
 59             else
 60             {
 61                 can[a][b][4] = 1;
 62                 can[x][y][3] = 1;
 63             }
 64         }
 65         else
 66         {
 67             if (a == x + 1)
 68             {
 69                 can[a][b][1] = 1;
 70                 can[x][y][2] = 1;
 71             }
 72             else
 73             {
 74                 can[a][b][2] = 1;
 75                 can[x][y][1] = 1;
 76             }
 77         }
 78     }
 79     
 80     for (register int i = 1 ; i <= k ; i ++)
 81     {
 82         int x = read(), y = read();
 83         cowx[i] = x, cowy[i] = y;
 84     }
 85     
 86     for (register int i = 1 ; i <= k ; i ++)
 87     {
 88         if (!color[cowx[i]][cowy[i]])
 89         {
 90             col++;
 91             bfs(cowx[i], cowy[i]);
 92         }
 93     }
 94     int res = 0 ;    
 95     for (register int i = 1 ; i <= k ; i ++)
 96     {
 97         for (register int j = i + 1 ; j <= k ; j ++)
 98         {
 99             if (color[cowx[i]][cowy[i]] != color[cowx[j]][cowy[j]]) res++;
100         }
101     }
102     printf("%d\n", res);
103     return 0;
104 }
 
颓颓颓了一晚上, 明天上课, 现在作业还没动, 凉凉;
不说了颓作业去;

猜你喜欢

转载自www.cnblogs.com/zZh-Brim/p/9119221.html