poj 1129 Channel Allocation——四色定理
轉載于:…(* ̄0 ̄)ノ
Channel Allocation
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.
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.
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 0Sample Output
1 channel needed. 3 channels needed. 4 channels needed.題意: 當一個廣播電臺在一個非常大的地區,廣播站會用中繼器來轉播信號以使得每一個接收器都能接收到一個強烈的信號。然而,每個中繼器必須慎重選擇使用,使相鄰的中繼器不互相干擾。如果相鄰的中繼器使用不同的頻道,那么就不會相互干擾。
由于無線電頻道是一有限的,一個給定的網絡所需的中繼頻道數目應減至最低。編寫一個程序,讀取一個中繼網絡,然后求出需要的最低的不同頻道數。
給出一些點以及這些點直接的連接關系,求最少可以用幾種顏色對這些點涂色,使得相鄰的點的顏色不同
解題思路:
對于這題數據范圍很小(節點最多26個),所以使用普通的暴力搜索法即可
對點i的染色操作:從最小的顏色開始搜索,當i的直接相鄰(直接后繼)結點已經染過該種顏色時,搜索下一種顏色。
就是說i的染色,當且僅當i的臨近結點都沒有染過該種顏色,且該種顏色要盡可能小。
要注意題中的一句話
since the repeaters lie in a plane, the graph formed byconnecting adjacent repeaters does not have any line segments that cross.
大致意思就是 “城市所在的世界是一個平面世界,當把城市看做點,相鄰城市用邊連接時,這些邊不能相交”
?
PS: 在網上很多同學都說用“四色定理”解決這題, 其實不是,四色定理在本題單純是用來剪枝的,而且由于結點數較少(只有26),剪枝與否對時間影響不大,普通的爆搜也是0ms AC的,不過有興趣的同學可以看看“四色定理”。
因為當結點數很多時,四色定理的剪枝優勢就會體現出來了
?
我把 暴力搜索 和 經過四色定理剪枝的搜索 兩個程序都都貼出來,大家比較一下就知道四色定理怎么用了。
?
附:
四色定理的“相鄰”是指兩塊多邊形地區“至少一條邊重合”才為之相鄰
“至少一條邊重合”同時也隱含了“任意邊(線段)不正規相交
如:
我舉一個例子就一目了然了:
N=7
A:BCDEFG
B:ACDEFG
C:ABD
D:ABCE
E:ABDF
F:ABEG
G:ABF
畫成圖就是:
PS:由于邊不允許相交,這已經是7個點的最大連接數
四色定理的原始理論依據:
對于一個散點集,若要求盡可能連接任意兩個點,但任意一條邊邊不允許與其他邊相交,
那么當散點集的元素個數<=4時,連接所得的圖必為一個一個 無向完全圖
當散點集的元素個數>4時,連接所得的圖必不是一個完全圖
完全圖:任意兩點均相鄰
?
最后千萬要注意輸出,當頻道數大于1時,channel為復數 channels
/*四色定理*///Memory Time //184K 0MS #include<iostream> using namespace std;typedef class {public:int next[27]; //直接后繼int pn; //next[]指針(后繼個數) }point;int main(int i,int j,int k) {int n;while(cin>>n){if(!n)break;getchar(); //n的換行符point* node=new point[n+1]; //結點/*Structure the Map*/for(i=1;i<=n;i++){getchar(); //結點序號getchar(); //冒號if(node[i].pn<0) //初始化指針node[i].pn=0;char ch;while((ch=getchar())!='\n'){j=ch%('A'-1); //把結點字母轉換為相應的數字,如A->1 C->3node[i].next[ ++node[i].pn ]=j;}}int color[27]={0}; //color[i]為第i個結點當前染的顏色,0為無色(無染色)color[1]=1; //結點A初始化染第1種色int maxcolor=1; //當前已使用不同顏色的種數for(i=1;i<=n;i++) //枚舉每個頂點{color[i]=n+1; //先假設結點i染最大的顏色bool vist[27]={false}; //標記第i種顏色是否在當前結點的相鄰結點染過for(j=1;j<=node[i].pn;j++) //枚舉頂點i的所有后繼{int k=node[i].next[j];if(color[k]) //頂點i的第j個直接后繼已染色vist[ color[k] ]=true; //標記該種顏色}for(j=1;i<=n;j++) //從最小的顏色開始,枚舉每種顏色if(!vist[j] && color[i]>j) //注意染色的過程是一個不斷調整的過程,可能會暫時出現大于4的顏色{ //因此不能單純枚舉4種色,不然會WAcolor[i]=j;break;}if(maxcolor<color[i]){maxcolor=color[i];if(maxcolor==4) //由四色定理知,最終完成染色后,圖上最多只有四種顏色break; //因此當染色過程出現結點的顏色為4時,就可以斷定最少要用4種顏色染色}}if(maxcolor==1)cout<<1<<" channel needed."<<endl;elsecout<<maxcolor<<" channels needed."<<endl;delete node;}return 0; }總結
以上是生活随笔為你收集整理的poj 1129 Channel Allocation——四色定理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C/C++笔试题(13)
- 下一篇: 网站favion.ico图标