牛客网 Touring cities

链接:https://www.nowcoder.com/acm/contest/146/E
来源:牛客网
 

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Niuniu wants to tour the cities in Moe country. Moe country has a total of n*m cities. The positions of the cities form a grid with n rows and m columns. We represent the city in row x and column y as (x,y). (1 ≤ x ≤ n,1 ≤ y ≤ m) There are bidirectional railways between adjacent cities. (x1,y1) and (x2,y2) are called adjacent if and only if |x1-x2|+|y1-y2|=1. There are also K bidirectional air lines between K pairs of cities. It takes Niuniu exactly one day to travel by a single line of railway or airplane. Niuniu starts and ends his tour in (1,1). What is the minimal time Niuniu has to travel between cities so that he can visit every city at least once

Note that the air line may start and end in the same city.

输入描述:

 

The first line contains oneinteger T(T≤20), which means the number of test cases.

Each test case has the format as described below.

n m K

ax1 ay1 bx1 by1
ax2 ay2 bx2 by2

axK ayK bxK byK

(0 ≤ K ≤ 10. 2 ≤ n,m ≤ 100, 1 ≤ n*m ≤ 100)

There is one bidirectional air line between (axi,ayi) and (bxi,byi). (1 ≤ axi,bxi ≤ n , 1 ≤ ayi,byi ≤ m)

 

输出描述:

For each test case,print one number in a single line, which is the minimal number of days Niuniu has to travel between cities so that he can visit every city at least once.

示例1

输入

复制

3
2 2 1
1 1 2 2
3 3 1
1 1 3 3
3 3 0

输出

复制

4
9
10

备注:

The air line may start and end in the same city.

 

给定一个n行m列的棋盘,将格子视为点,相邻的格子对应的点间连一条边。再此基础上新加k条边,形成一个n*m个点的无向图。问经过每个点至少一次的环路长度最短是多少。(n,m>1)

 

由于答案最多为n*m+1,可以将题目转换为判断是否存在哈密尔顿回路。

 

如果n和m有一个是偶数,那么必定存在哈密尔顿回路。

 

如果n和m都是奇数:

将棋盘黑白染色,规定左上角的格子是黑色。那么存在哈密尔顿回路的条件是当且仅当新增的边连接了两个不同的黑色格子。

 

考虑证明:

若没有连接两个不同黑格子的边,那么回路上一个黑格子后必然接一个白格子。那么回路的黑格子数目一定小于等于白格子数目。但是棋盘上的黑格子数目比白格子多一。矛盾。

 

若存在一个连接两个不同黑格子的边,那么一定存在一条合法的方案。

两个思路:

1)按照起点终点位置分类讨论构造

2)归纳证明 

若有一维大于5,一定可以将其减2

归纳到n,m<=5的情况,此时可以自行验证

 

总的来说就是判断飞机航班的是否存在两段都是(x+y)%2==0的现象

#include <cstdio>
#include <cstring>
using namespace std;
int main() {
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        int s=0;
        int x1,x2,y1,y2;
        for(int i=1;i<=k;i++){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            if((x1+y1)%2==0&&(x2+y2)%2==0&&(x1!=x2||y1!=y2))
                s=1;
        }
        if((n%2==0||m%2==0))
            s=1;
        if(s)
            printf("%d\n",n*m);
        else
            printf("%d\n",n*m+1);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值