Channel Allocation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 15064 | Accepted: 7655 |
Description
When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels.
Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.
Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.
Input
The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,...,I and J. A network with zero repeaters indicates the end of input.
Following the number of repeaters is a list of adjacency relationships. Each line has the form:
A:BCDH
which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form
A:
The repeaters are listed in alphabetical order.
Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross.
Following the number of repeaters is a list of adjacency relationships. Each line has the form:
A:BCDH
which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form
A:
The repeaters are listed in alphabetical order.
Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross.
Output
For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.
Sample Input
2 A: B: 4 A:BC B:ACD C:ABD D:BC 4 A:BCD B:ACD C:ABD D:ABC 0
Sample Output
1 channel needed. 3 channels needed. 4 channels needed.
题意就是 给你几个点 相邻的不能同一种信道(颜色)
问最少几种信道全部联通(染完色)
利用四色 分别用了两种解法
dfs 四色
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
#define For(i,a,b) for(i=a;i<=b;i++)
#define _For(i,a,b) for(i=b;i>=a;i--)
#define Out(x) cout<<x<<endl
#define mset(arr,num) memset(arr,num,sizeof(arr))
#define ll long long
const ll inf = 2000; ///
#define ok std::ios::sync_with_stdio(0)
#ifndef SYMBOL
#define SYMBOL value
#endif
// #define debug
#if defined (debug)
---check---
#endif
///
int mp[30][30]; ///图
int color[30]; ///颜色序号
char str[30];
bool flag;
int maxn,n;
void Find(int key) ///应用四色定理剪枝
{
int i,Numbcolor;
if(key > n) ///如果找完了最后一个点
{
flag = 1; ///标记flag为1 表示有答案了
For(i,1,n) maxn = max(maxn,color[i]);
if(maxn == 1)
{
Out("1 channel needed.");
}
else
{
cout<<maxn<<" channels needed."<<endl;
}
return ;
}
For(Numbcolor,1,4) ///其实这就是剪枝过程
{
For(i,1,n)
{
if(mp[key][i] && color[i] == Numbcolor)
{
break; ///如果这个颜色用过 那么就不用这个颜色
}
}
if(i > n)
{
color[key] = Numbcolor; ///染这个颜色
Find(key+1); ///寻找下一个点
if(flag) ///找到答案了 直接return
{
return ;
}
color[key] = 0; ///到了这一步说明刚才的颜色不合理
} ///因为四色所以不会走到的23333 所以没有回溯直接for即可 见后文
}
}
int main()
{
int i,j;
ok;
while(cin>>n,n)
{
flag = 0;
maxn = 0;
mset(mp,0);
mset(color,0);
For(i,1,n)
{
cin>>str;
For(j,2,strlen(str)-1)
{
mp[str[0]-'A'+1][str[j]-'A'+1] = 1;
mp[str[j]-'A'+1][str[0]-'A'+1] = 1; ///建立无向图 A-1 B-2 ...
}
}
Find(1);
}
return 0;
}
枚举 四色
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
#define For(i,a,b) for(i=a;i<=b;i++)
#define _For(i,a,b) for(i=b;i>=a;i--)
#define Out(x) cout<<x<<endl
#define mset(arr,num) memset(arr,num,sizeof(arr))
#define ll long long
const ll inf = 2000; ///
#define ok std::ios::sync_with_stdio(0)
#ifndef SYMBOL
#define SYMBOL value
#endif
// #define debug
#if defined (debug)
---check---
#endif
///
bool mp[28][28]; ///图
short color[30]; ///颜色序号
char str[30];
int n;
void Find() ///直接应用四色定理枚举
{
int j,i,Numbcolor;
int maxn = 0;
For(j,1,n)
{
For(Numbcolor,1,4)
{
For(i,1,n)
{
if(mp[j][i] && color[i] == Numbcolor)
{
break; ///如果这个颜色用过 那么就不用这个颜色
}
}
if(i > n)
{
color[j] = Numbcolor; ///染这个颜色之后跳出
break;
}
maxn = max(maxn,Numbcolor);
if(maxn == 4) ///四色优化 - 用完四色直接打印
{
cout<<maxn<<" channels needed."<<endl;
return ;
}
}
}
maxn += 1;
if(maxn == 1)
{
Out("1 channel needed.");
}
else
{
cout<<maxn<<" channels needed."<<endl;
}
}
int main()
{
int i,j;
ok;
while(cin>>n,n)
{
mset(mp,0);
mset(color,0);
For(i,1,n)
{
cin>>str;
For(j,2,strlen(str)-1)
{
mp[str[0]-'A'+1][str[j]-'A'+1] = 1;
mp[str[j]-'A'+1][str[0]-'A'+1] = 1; ///建立无向图 A-1 B-2 ...
}
}
Find();
}
return 0;
}