[Usaco2009 Nov]lights 燈

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sdj222555/article/details/14521393

又是一关于操作灯开关的题目。

一眼看去就知道是高斯消元。

但是,该题的重点是输出一个最小操作数的解!


这就需要进行枚举自由变元了!

如果学习过线性代数的话就知道了。

将矩阵转为下三角矩阵后。

有一种东西叫关键元。就是每一行的第一个非零元。

通常来讲这个第i行的关键元应该在第i列才对。

否则就会出现多解的问题。

那么如果多解的话。这个本来应该是关键元的地方就成了自由变元了。

就需要枚举这个位置的值,还好只有1,0两种。

直接从最下面一行DFS深搜,然后剪枝即可。

参考了 一个文章

http://blog.sina.com.cn/s/blog_51cea4040100g7hl.html

代码也是扒的

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <cmath>
#define MAXN 200115
#define MAXM 500005
#define INF 1000000000
using namespace std;
int mp[55][55];
int m[55][55];
int b[55];
int x[55];
int n, M;
void build()
{
    for(int i = 1; i <= n; i++) b[i] = 1;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            m[i][j] = mp[i][j];
}
void Swap(int i, int j)
{
    for(int k = 1; k <= n; k++) swap(m[i][k], m[j][k]);
    swap(b[i], b[j]);
}
void Xor(int i, int j)
{
    for(int k = 1; k <= n; k++) m[i][k] ^= m[j][k];
    b[i] ^= b[j];
}
int ans, v[55];
void dfs(int k, int num)
{
    if(num >= ans) return;
    if(k == 0)
    {
        if(num < ans) ans = num;
        return;
    }
    if(m[k][k])
    {
        int now = b[k];
        for(int i = k + 1; i <= n; i++)
            if(m[k][i]) now ^= v[i];
        v[k] = now;
        if(v[k]) dfs(k - 1, num + 1);
        else dfs(k - 1, num);
    }
    else
    {
        v[k] = 0; dfs(k - 1, num);
        v[k] = 1; dfs(k - 1, num + 1);
        v[k] = 0;
    }
}
void work()
{
    for(int k = 1; k <= n; k++)
    {
        bool flag = false;
        for(int i = k; i <= n; i++)
            if(m[i][k])
            {
                flag = true;
                Swap(i, k);
                break;
            }
        if(flag == false) continue;
        for(int i = k + 1; i <= n; i++)
            if(m[i][k]) Xor(i, k);
    }
    ans = INF;
    dfs(n, 0);
    printf("%d\n", ans);
}
int main()
{
    int u, v;
    scanf("%d%d", &n, &M);
    for(int i = 1; i <= n; i++) mp[i][i] = 1;
    for(int i = 1; i <= M; i++)
    {
        scanf("%d%d", &u, &v);
        mp[u][v] = mp[v][u] = 1;
    }
    build();
    work();
    return 0;
}



Ballroom Lights Ballroom Lights

09-23

Problem DescriptionrnThe ICPC world finals will be held in a luxurious hotel with a big ballroom. A buffet meal will be served in this ballroom, and organizers decided to decorate its walls with pictures of past champion teams.rnrnIn order to avoid criticism about favouring some of those teams over others, the organizing commitee wants to make sure that all pictures are appropiately illuminated. rnrnThe only direct way they’ve found for doing this is ensuring each picture has at least one lightbulb that directlyilluminates it.rnrnIn this way, the perimeter of the ballroom wall can be divided into illuminated parts (in whichpictures may be placed) and dark parts (which are not suitable for placing the pictures).rnrnThe ballroom has the shape of a box and contains several lightbulbs. Each lightbulb emits light in all directions, but this light can be blocked by columns. All columns in the ballroom have cylindrical shape and go from the floor to the ceiling, so light cannot pass over or above them. Columns are of course placed so that its circular section is parallel to the ballroom floor.rnrnAny given point p on the perimeter wall is said to be illuminated if there exists a line segment (a light ray) which starts on a lightbulb, ends in p and does not touch or pass through any column.rnrnrnYour task as a helper of the ICPC organization is to examine the blueprints of the ballroom and determine the total length of illuminated sections of the perimeter wall. The blueprint consist of a rectangle indicating a top view of the ballroom, with the lightbulbs and columns marked in it.rn rnrnInputrnEach test case will consist on several lines. The first line will contain four integers: L, the number of lightbulbs, C, the number of columns, X, the size of the ballroom on the x coordinate and Y , the size of the ballroom on the y coordinate. The lower-left corner of the ballroom is at (0, 0) while the upper-right corner is at (X, Y ).rnrnThe next L lines will contain two integers each representing the x and y coordinate of each lightbulb. The last C lines of the test case will contain three integers each, representing the x and y coordinates of the center of a column and its radius, in that order. You can assume that 1 <= L,C <= 103 and 4 <= X, Y <= 106. Also, for all pairs of coordinates (x,y), 0 < x < X and 0 < y < Y , both for lightbulbs and column center locations. All radii of the columns will be positive. Finally, no two columns will overlap, although they may touch, and no column will touch or intersect with the border of the ballroom. No lightbulb will be inside a column or in its boundary and no two lightbulbs will be in the same place.rnInput is terminated with L = C = X = Y = 0.rn rnrnOutputrnFor each test case, output a single line with the total length of the illuminated parts of the perimeter wall. The result must be printed as a real number with exactly four decimal figures, with the lowest-order decimal figure rounded up.rn rnrnSample Inputrn2 1 8 8rn6 6rn2 6rn4 4 2rn1 4 7 7rn3 3rn2 4 1rn4 2 1rn2 2 1rn4 4 1rn2 2 9 7rn1 2rn5 5rn3 3 2rn7 5 1rn0 0 0 0rn rnrnSample Outputrn28.0000rn0.0000rn25.8214

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭