Sha 10160 Servicing Stations

Source: Internet
Author: User

UVA_10160

I changed this question to the Dancing Links algorithm. I did not expect the search to pass, but it took a lot of time.

Because the original Dancing Links solves the problem of precise coverage, and this problem can be overwritten repeatedly, we need to change the delete and restore operations of the Dancing Links.

Of course, there are more efficient search methods, You can google it.

#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
const int MAXN = 40;
const int MAXD = MAXN * MAXN + MAXN;
int U[MAXD], D[MAXD], L[MAXD], R[MAXD], C[MAXD], H[MAXN], S[MAXN];
int vis[MAXN], del[MAXN][MAXN], N, M, Min, size, g[MAXN][MAXN];
void prepare(int r, int c)
{
int i;
for(i = 0; i <= c; i ++)
{
U[i] = D[i] = i;
L[i + 1] = i, R[i] = i + 1;
S[i] = 0;
}
R[c] = 0;
while(r)
H[r --] = -1;
size = c;
}
void insert(int r, int c)
{
++ size;
C[size] = c;
++ S[c];
U[size] = c;
D[size] = D[c];
U[D[c]] = size;
D[c] = size;
if(H[r] == -1)
H[r] = L[size] = R[size] = size;
else
{
L[size] = H[r];
R[size] = R[H[r]];
L[R[H[r]]] = size;
R[H[r]] = size;
}
}
void remove(int c)
{
R[L[c]] = R[c];
L[R[c]] = L[c];
}
void resume(int c)
{
R[L[c]] = c;
L[R[c]] = c;
}
void dance(int cur)
{
int i, j, c, min = INF;
if(!R[0])
{
if(cur < Min)
Min = cur;
return ;
}
if(cur >= Min - 1)
return ;
for(i = R[0]; i != 0; i = R[i])
if(S[i] < min)
{
min = S[i];
c = i;
}
del[cur][c] = vis[c] = 1;
remove(c);
for(i = D[c]; i != c; i = D[i])
{
for(j = R[i]; j != i; j = R[j])
if(!vis[C[j]])
{
vis[C[j]] = del[cur][C[j]] = 1;
remove(C[j]);
}
dance(cur + 1);
for(j = L[i]; j != i; j = L[j])
if(del[cur][C[j]])
{
resume(C[j]);
vis[C[j]] = del[cur][C[j]] = 0;
}
}
resume(c);
del[cur][c] = vis[c] = 0;
}
void solve()
{
int i, j, k, x, y;
prepare(N, N);
memset(g, 0, sizeof(g));
for(i = 1; i <= N; i ++)
g[i][i] = 1;
for(i = 0; i < M; i ++)
{
scanf("%d%d", &x, &y);
g[x][y] = g[y][x] = 1;
}
for(i = 1; i <= N; i ++)
for(j = 1; j <= N; j ++)
if(g[i][j])
insert(i, j);
Min = INF;
memset(del, 0, sizeof(del));
memset(vis, 0, sizeof(vis));
dance(0);
printf("%d\n", Min);
}
int main()
{
for(;;)
{
scanf("%d%d", &N, &M);
if(!N && !M)
break;
solve();
}
return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.