锘??xml version="1.0" encoding="utf-8" standalone="yes"?> PKU 1018 Communication System 綆楁硶鍒嗙被錛?/span> 鏋氫婦+璐績(jī) 鍘熸枃錛?/span> Communication System Time
Limit:1000MS Memory Limit:10000K Description We have received an order from
Pizoor Communications Inc. for a special communication system. The system
consists of several devices. For each device, we are free to choose from
several manufacturers. Same devices from two manufacturers differ in their
maximum bandwidths and prices. Input The first line of the input file
contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by
the input data for each test case. Each test case starts with a line containing
a single integer n (1 ≤ n ≤ 100), the number of devices in the communication
system, followed by n lines in the following format: the i-th line (1 ≤ i ≤ n)
starts with mi (1 ≤ mi ≤ 100), the number of manufacturers for the i-th device,
followed by mi pairs of positive integers in the same line, each indicating the
bandwidth and the price of the device respectively, corresponding to a
manufacturer. Output Your program should produce a single
line for each test case containing a single number which is the maximum
possible B/P for the test case. Round the numbers in the output to 3 digits
after decimal point. Sample Input 1
3 3
100 25 150 35 80 25 2
120 80 155 40 2
100 100 120 110 Sample Output 0.649 Source Tehran 2002, First Iran Nationwide
Internet Programming Contest 涓枃鎻忚堪錛?/span> 浣犻渶瑕佽喘涔?/span>n縐嶈澶囨潵緇勪竴涓氫俊緋葷粺錛屾瘡涓縐嶈澶囷紝鍙堟槸鐢變竴浜涗笉鍚岀殑鍒墮犲晢鐢熶駭鐨勶紝涓嶅悓鍒墮犲晢鐢熶駭鐨勫悓縐嶈澶囦細(xì)鏈変笉鍚岀殑甯﹀鍜屼環(huán)鏍箋傜幇鍦ㄤ綘瑕佸湪姣忎竴涓澶囩殑鍒墮犲晢涓変竴涓紝浣垮緱璐拱鐨?/span>n縐嶈澶囷紝瀹冧滑甯﹀鐨勬渶灝忓間笌浠鋒牸涔嬪拰鐨勬瘮鏈澶с?/span> 棰樼洰鍒嗘瀽錛?/span> 涓寮濮嬫兂鍒扮殑灝辨槸鏆存悳錛屼絾鏄悳绱㈢殑娣卞害杈懼埌100錛屾椂闂磋偗瀹氭槸涓嶅厑璁哥殑銆傛兂瑕佽В鍐寵繖棰橈紝蹇呴』鎵懼埌涓涓ソ鐨勬煡鎵劇瓥鐣ャ傚啀鎯蟲(chóng)兂鐪嬭繖棰樼殑鐗圭偣錛屾渶鍚庣殑絳旀錛屽甫瀹芥槸閫夊彇鎵鏈夎澶囦腑鐨勬渶灝忓鹼紝鑰屼環(huán)鏍兼槸閫夊彇鎵鏈夎澶囩殑浠鋒牸鎬誨拰銆傚鏋滄煇涓埗閫犲晢鐢熶駭鐨勬煇縐嶈澶囷紝瀹冪殑甯﹀杈冮珮鑰屼環(huán)鏍艱緝浣庯紝閭d箞閫夊彇瀹冪殑鍙兘鎬у氨姣旇緝澶с傚啀榪涗竴姝ヨ錛屽鏋滄墍閫夊彇鐨?/span>n縐嶈澶囩殑甯﹀鐨勬渶灝忓?/span>b宸茬粡紜畾錛岄偅涔堝浜庢煇縐嶈澶囷紝鎴戜滑灝卞彲浠ュ湪閭d簺鐢熶駭榪欑璁懼鐨勶紝甯﹀澶т簬絳変簬b鐨勫埗閫犲晢涓繘琛岄夋嫨銆傚綋鐒舵槸閫夐偅涓環(huán)鏍兼渶浣庣殑璁懼錛屽洜涓虹瓟妗堢殑鍒嗗瓙宸茬粡紜畾涓?/span>b錛屾墍浠ュ垎姣嶈秺灝忚秺濂姐傜湅鏉ュ彧瑕佹灇涓?/span>b錛屽啀瀵逛簬姣忎釜璁懼璐績(jī)鐨勯夋嫨鏈灝忎環(huán)鏍煎氨鍙互浜?jiǎn)锛屾棄櫁村鏉傚害湄?fù)O錛?/span>mnB錛夛紝B涓哄甫瀹芥灇涓劇殑鏁伴噺銆備絾闂鍙堟潵浜?jiǎn)锛屽簲璇ユ庝箞鏋氫婦甯﹀錛岄鐩腑騫舵湭緇欏嚭甯﹀鐨勫彇鍊艱寖鍥達(dá)紝濡傛灉浠?/span>0..maxB涓涓竴涓灇涓劇殑璇濓紝鏃㈣垂鏃跺張浼?xì)閫犳垚榪囧閲嶅鎯呭喌錛堝鏋滄灇涓鵑偅浜涘湪杈撳叆涓嚭鐜扮殑涓や釜榪炵畫(huà)甯﹀涔嬮棿鐨勫鹼紝鏈鍚庣殑絳旀鏄竴鏍風(fēng)殑錛夈傛墍浠ユ垜浠簲璇ラ噰鍙栨煇涓柟娉曡褰曡緭鍏ヤ腑鍑虹幇榪囩殑甯﹀錛?/span>STL涓殑set鏄釜涓嶉敊鐨勯夋嫨錛夛紝鍐嶆灇涓捐繖浜涘甫瀹姐傚湪鏋氫婦涓紝鍙兘鍑虹幇榪欑鎯呭喌錛氭灇涓?/span>b錛岄夋嫨浜?/span>n縐嶈澶囷紝浣嗛夋嫨鐨勬墍鏈夎澶囩殑甯﹀閮藉ぇ浜?/span>b錛岄偅涔堟渶緇堢敤b/price灝變笉鏄繖縐嶆儏鍐電殑姝g‘絳旀銆傚叾瀹炰笉鐢ㄦ媴蹇?jī)锛屽洜湄?fù)姝g‘絳旀涓瀹氬ぇ浜?/span>b/price銆傚亣璁句笂闈㈣繖縐嶆儏鍐電殑瀹為檯甯﹀鏈灝忓兼槸b’錛岄偅涓綋鎴戜滑鍐嶅幓鏋氫婦b’鏃訛紝鑷沖皯鏈変竴涓澶囩殑甯﹀絳変簬b’錛岃繖嬈″緱鍒扮殑絳旀涔熷氨鏄笂闈㈤偅縐嶆儏鍐電殑絳旀錛屾墍浠ユ渶緇堣繕鏄兘寰楀埌姝g‘瑙c?/span> 浠g爜錛?/span> #include
<iostream> #include
<map> #include
<set> #include
<climits> using
namespace std; const int
MAX = 105; struct Info { int band, price; }; struct
Device { int manuNum; Info info[MAX]; map<int, int> minPrice; //map[i] = j 琛ㄧず甯﹀>=i鐨勬渶灝忎環(huán)鏍兼槸j int minBand, maxBand; }; Device
device[MAX]; int
deviceNum; set<int>
band; //杈撳叆涓嚭鐜拌繃鐨?/span>band set<int>::iterator
start, end; int
maxBand, minBand; //闇瑕佹灇涓劇殑band鐨勬渶鍊?/span> int cmp(
const void *a , const void *b ) { Info *c = (Info *)a; Info *d = (Info *)b; if(c->band != d->band) return
d->band - c->band; else return
c->price - d->price; } void Input
() { int i, j, max, min; band.clear(); cin>>deviceNum; for (i=0; i<deviceNum; i++) { device[i].minBand
= INT_MAX; device[i].maxBand
= -1; cin>>device[i].manuNum; for (j=0;
j<device[i].manuNum; j++) { cin>>device[i].info[j].band>>device[i].info[j].price; band.insert(device[i].info[j].band); if
( device[i].info[j].band > device[i].maxBand ) device[i].maxBand
= device[i].info[j].band; if
( device[i].info[j].band < device[i].minBand ) device[i].minBand
= device[i].info[j].band; } } } void Pre () //棰勫鐞?/span> { int i, j, min, b; //璁$畻鎵闇鏋氫婦鐨勫甫瀹界殑鏈鍊?/span> maxBand = INT_MAX; //maxBand涓烘墍鏈夎澶囧甫瀹芥渶澶у肩殑鏈灝忓?/span> minBand = INT_MAX; //minBand涓烘墍鏈夎澶囧甫瀹芥渶灝忓肩殑鏈灝忓?/span> for (i=0; i<deviceNum; i++) { if (
device[i].maxBand < maxBand ) maxBand
= device[i].maxBand; if (
device[i].minBand < minBand ) minBand
= device[i].minBand; } //瀵逛簬姣忎釜璁懼錛屾壘鍒板甫瀹藉ぇ浜庣瓑浜庢煇涓鍊肩殑鏈灝忎環(huán)鏍?/span> for (i=0; i<deviceNum; i++) { //band浠庡ぇ鍒板皬錛?/span>band鐩哥瓑鏃?/span>price浠庡皬鍒板ぇ qsort(device[i].info,
device[i].manuNum, sizeof(Info), cmp); device[i].minPrice.clear(); min =
device[i].info[0].price; b =
device[i].info[0].band; device[i].minPrice[b]
= min; for (j=1;
j<device[i].manuNum; j++) { if
( device[i].info[j].band == b ) continue; if
( device[i].info[j].price < min ) { min
= device[i].info[j].price; } b
= device[i].info[j].band; device[i].minPrice[b]
= min; } } } void Solve
() { Pre(); int b, i, totalPrice; double rate, ans; map<int, int>::iterator
it; ans = 0; start = band.find(minBand); end = band.find(maxBand); end ++; while ( start != end ) { b = *start; start ++; totalPrice = 0; for (i=0;
i<deviceNum; i++) { //鎵懼埌甯﹀澶т簬絳変簬b鐨勬渶灝忎環(huán)鏍?/span> for
(it=device[i].minPrice.begin(); it!=device[i].minPrice.end(); it++) { if
( it->first >= b ) { totalPrice
+= it->second; break; } } } rate = double(b)
/ totalPrice; if ( rate >
ans ) ans
= rate; } printf("%.3f\n", ans); } int main () { int test; cin>>test; while ( test -- ) { Input (); Solve (); } return 0; }
鎬濊礬錛?br> 鍛婅瘔浣燵a,b]涔嬮棿1涓暟鐨勫鍋舵儏鍐碉紝閭d箞浣犲氨鍙互鍦╝-1鍜宐涔嬮棿榪炰竴鏉¤竟錛屾潈鍊煎氨鏄叾濂囧伓鎯呭喌銆傝繖鏍蜂竴鏉ワ紝姣斿[1,2]鍜孾3,4]鐨勬儏鍐靛凡鐭ワ紝[1,4]鐨勬儏鍐典篃灝辯煡閬撲簡(jiǎn)銆傚綋棰樼洰緇欏嚭[a,b]鐨勬儏鍐墊椂錛岄鍏堝垎鍒粠a鍜宐寰涓婃壘錛屾壘鍒頒粬浠殑鏍箁1鍜宺2錛屽鏋渞1 != r2錛岃〃紺篴,b涔嬮棿鐨勫鍋舵儏鍐佃繕?shù)笉纭畾锛尀灏唕1鍜宺2涔嬮棿榪炶搗鏉ワ紝鏍規(guī)嵁a(chǎn)鍒皉1鐨勬潈鍊箋乥鍒皉2鐨勬潈鍊煎拰棰樼洰鎵緇欑殑濂囧伓鎯呭喌錛岃緗畆1鍜宺2涔嬮棿鐨勬潈鍊鹼紝浠ョ鍚堥鐩姹傘傝嫢r1 == r2錛屽垯琛ㄧず[a,b]涔嬮棿鎯呭喌宸茬‘瀹氾紝鏍規(guī)嵁a(chǎn)鍒皉1鐨勬潈鍊煎拰b鍒皉2鐨勬潈鍊鹼紝灝卞彲浠ュ垽鏂鐩墍緇欑殑[a,b]鐨勬儏鍐墊槸鍚︿負(fù)鐪熴?br> 鍏跺疄褰撴椂鍋氱殑鏃跺欙紝榪樹(shù)笉鏄緢鎳傦紝浣嗘病鎯沖埌紼閲岀硦娑傜殑灝盇C浜?jiǎn)銆傛帹鑽愪竴涓嬭繖涓綉欏碉細(xì)http://hi.baidu.com/fandywang_jlu/blog/item/b49e40893ddbb0b00f244485.html錛岃繖閲岄潰浠嬬粛騫舵煡闆嗘尯璇︾粏鐨勶紝榪樻湁涓嶅皯鎺ㄨ崘棰樼洰錛屾湁浜涜繕?shù)笉浼?xì)鍋氥傦細(xì)P
浠g爜錛?br>
#include <iostream>
#include <map>
using namespace std;
const int MAX = 10005;
int n, p;
int pre[MAX];
int parity[MAX]; //i鍒扮洰鍓嶉泦鍚堢殑鏍圭殑濂囧伓鎯呭喌
map<int, int> numIndex; //鐢ㄤ簬紱繪暎鍖?br>
int Find (int x)
{
if ( pre[x] == -1 )
return x;
int f;
f = Find(pre[x]);
parity[x] = (parity[x] + parity[pre[x]]) % 2; //姝ゆ椂pre[x]宸叉寚鍚戞渶緇堢殑闆嗗悎鐨勬牴
pre[x] = f;
return f;
}
bool Query (int x, int y, int odd)
{
int r1, r2;
r1 = Find(x);
r2 = Find(y);
if ( r1 == r2 )
{
if ( (parity[x] + parity[y]) % 2 == odd )
return true;
else
return false;
}
else //鍙槸灝唕1鎺ュ埌r2涓嬮潰錛岃繖杈硅繕鍙互浼樺寲
{
pre[r1] = r2;
parity[r1] = (parity[x] + parity[y] + odd) % 2;
return true;
}
}
void Solve ()
{
int i, x, y, index, idx1, idx2, odd;
char s[10];
scanf("%d%d", &n, &p);
index = 0;
for (i=0; i<p; i++)
{
scanf("%d%d%s", &x, &y, &s);
x --;
if ( numIndex.find(x) == numIndex.end() )
numIndex[x] = index ++;
idx1 = numIndex[x];
if ( numIndex.find(y) == numIndex.end() )
numIndex[y] = index ++;
idx2 = numIndex[y];
if ( strcmp(s, "odd") == 0 )
odd = 1;
else
odd = 0;
if ( Query(idx1, idx2, odd) == false )
{
break;
}
}
printf("%d\n", i);
}
void Init ()
{
memset(pre, -1, sizeof(pre));
}
int main ()
{
Init();
Solve();
return 0;
}
]]>
]]>
]]>
]]>
By overall bandwidth (B) we mean the minimum of the bandwidths of the chosen
devices in the communication system and the total price (P) is the sum of the
prices of all chosen devices. Our goal is to choose a manufacturer for each
device to maximize B/P.
]]>
]]>
棰樼洰鏉ユ簮錛?/span>
PKU 1505 Copying Books
綆楁硶鍒嗙被錛?/span>
DP
鍘熸枃錛?/span>
Copying Books
Time Limit: 3000MS |
Memory Limit: 10000K |
|
Total Submissions: 1806 |
Accepted: 404 |
Description
Before the
invention of book-printing, it was very hard to make a copy of a book. All the
contents had to be re-written by hand by so called scribers. The scriber had
been given a book and after several months he finished its copy. One of the
most famous scribers lived in the 15th century and his name was Xaverius
Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying
and boring. And the only way to speed it up was to hire more scribers.
Once upon a time, there was a theater ensemble that wanted to play famous
Antique Tragedies. The scripts of these plays were divided into many books and
actors needed more copies of them, of course. So they hired many scribers to
make copies of these books. Imagine you have m books (numbered 1, 2 ... m) that
may have different number of pages (p1, p2 ... pm) and you want to make one
copy of each of them. Your task is to divide these books among k scribes, k
<= m. Each book can be assigned to a single scriber only, and every scriber
must get a continuous sequence of books. That means, there exists an increasing
succession of numbers 0 = b0 < b1 < b2, ... < bk-1 <= bk
= m such that i-th scriber gets a sequence of books with numbers between bi-1+1
and bi. The time needed to make a copy of all the books is determined by the
scriber who was assigned the most work. Therefore, our goal is to minimize the
maximum number of pages assigned to a single scriber. Your task is to find the
optimal assignment.
Input
The input
consists of N cases. The first line of the input contains only positive integer
N. Then follow the cases. Each case consists of exactly two lines. At the first
line, there are two integers m and k, 1 <= k <= m <= 500. At the
second line, there are integers p1, p2, ... pm separated by spaces. All these
values are positive and less than 10000000.
Output
For each
case, print exactly one line. The line must contain the input succession p1,
p2, ... pm divided into exactly k parts such that the maximum sum of a single
part should be as small as possible. Use the slash character ('/') to separate
the parts. There must be exactly one space character between any two successive
numbers and between the number and the slash.
If there is more than one solution, print the one that minimizes the work
assigned to the first scriber, then to the second scriber etc. But each scriber
must be assigned at least one book.
Sample Input
2
9
3
100
200 300 400 500 600 700 800 900
5
4
100
100 100 100 100
Sample Output
100
200 300 400 500 / 600 700 / 800 900
100
/ 100 / 100 / 100 100
Source
涓枃鎻忚堪錛?/span>
棰樼洰澶ф剰鏄粰浣?/span>m錛?/span>1…m錛夋湰涔︼紝姣忔湰涔︽湁Pm欏碉紝鐢?/span>k錛?/span>k<=m錛変釜鍛樺伐鏉ュ鍗拌繖浜涗功銆傛瘡鏈功鍙兘鍒嗛厤緇欎竴涓憳宸ユ潵澶嶅嵃錛屽茍涓旀瘡涓憳宸ュ繀欏誨鍗頒竴孌佃繛緇殑涔︾睄錛屾瘡涓憳宸ュ鍗扮殑鏃墮棿鍙栧喅浜庢墍澶嶅嵃涔︾睄鐨勬婚〉鏁般傝浣犵粰鍑虹浉搴旂殑鍒嗛厤錛屼嬌寰楀垎閰嶇粰鍛樺伐鐨勪功綾嶉〉鏁扮殑鏈澶у煎敖閲忓皬銆傛敞鎰忥紝濡傛灉鏈夊縐嶅垎閰嶇殑鏂規(guī)錛屼嬌寰楃涓涓憳宸ョ殑涔︾睄欏墊暟灝介噺灝戯紝鍏舵鏄浜屼釜銆佺涓変釜……浠ユ綾繪帹銆?/span>
棰樼洰鍒嗘瀽錛?/span>
鎴戜滑鍙互浠庡悗寰鍓嶆帹錛屾渶鍚庝竴涓憳宸ワ紝涔熷氨鏄k涓憳宸ワ紝浠栬嚦灝戣澶嶅嵃絎?/span>m鏈功錛岃嚦澶氬彲浠ュ鍗扮k鏈埌絎?/span>m鏈紙鍥犱負(fù)鑷沖皯瑕佸垎閰嶇粰鍓?/span>k-1涓憳宸ユ瘡浜轟竴鏈功錛夈傚亣璁撅紝絎?/span>k鍚嶅憳宸ュ鍒剁i錛?/span>k<=i<=m錛夋湰涔﹀埌絎?/span>m鏈功錛岄偅涔堬紝鎵鏈夊憳宸ュ鍗頒功綾嶇殑鏈灝忔椂闂村氨涓虹k鍚嶅憳宸ユ墍闇鐨勬椂闂翠互鍙?qiáng)鍓?/span>k-1鍚嶅憳宸ュ鍒跺墠i-1鏈功鎵闇鏈灝忔椂闂寸殑杈冨ぇ鐨勯偅涓椂闂淬傝繖鏍鳳紝闂鐨勮妯″氨浠?/span>k涓憳宸ュ鍗?/span>m鏈功鍑忓皬鍒頒簡(jiǎn)k-1涓憳宸ュ鍗?/span>i-1鏈功錛岃屼笖姹傝В榪囩▼涓細(xì)涓嶆柇閬囧埌鍓?/span>a涓憳宸ュ鍗板墠b鏈功鐨勬渶灝忔椂闂淬備負(fù)浜?jiǎn)鍑彏畯闂鐨勮妯′互鍙?qiáng)璁板綍閲嶅瀛愰棶棰樼殑瑙o紝灝卞彲浠ョ敤DP銆?/span>
浣嗕粎浠呯畻鍑烘渶灝忔椂闂寸殑涓嶅鐨勶紝榪樿緇欏嚭鍒嗛厤鐨勬柟妗堬紝榪欎釜紼嶅井鏈夌偣綣佺悙銆傚洜涓洪鐩腑璇達(dá)紝濡傛灉鏈夊縐嶆渶浼樼殑鍒嗛厤鏂規(guī)錛屽簲璇ヨ鍓嶉潰鐨勫憳宸ュ垎閰嶇殑欏墊暟灝介噺灝戙傞偅涔堬紝鍙互浠庡悗鎺紝鍦ㄥ綋鍓嶇殑鍛樺伐鎵澶嶅嵃鐨勪功綾嶉〉鏁版病鏈夎秴榪囨渶澶ч〉鏁扮殑鎯呭喌涓嬶紝璁╁叾澶嶅嵃鐨勯〉鏁版渶澶у寲銆傚鏋滆秴榪囦簡(jiǎn)鏈澶ч〉鏁幫紝灝辨妸榪欐湰涔﹀垎閰嶇粰鍓嶄竴鍚嶅憳宸ャ傛渶鍚庡啀鎸夐『搴忓皢鍒嗛厤緇撴灉杈撳嚭鍑烘潵銆?/span>
浠g爜錛?/span>
#include
<cstdio>
#include
<climits>
#include
<cstring>
const int
MAX = 505;
int
book[MAX];
__int64
total[MAX]; //1~n鏈功鐨勯〉鏁?/span>
int k, m;
__int64
f[MAX][MAX]; //f[i][j] = k 琛ㄧず鍓?/span>i涓漢澶嶅埗鍓?/span>j鏈功鎵闇鏈灝戞椂闂存槸k
__int64
max;
void Input
()
{
scanf("%d%d", &m,
&k);
int i;
for (i=1; i<=m; i++)
scanf("%d",
&book[i]);
}
__int64 Sum
(int s, int e) //絎?/span>s鏈功鍒扮e鏈功鐨勬婚〉鏁?/span>
{
return (total[e] - total[s-1]);
}
__int64 Max
(__int64 a, __int64 b)
{
return ( a>b?a:b );
}
__int64 Min
(int x, int y) //鍓?/span>x涓漢澶嶅埗鍓?/span>y鏈功鎵闇鐨勬渶灝戞椂闂?/span> x<=y
{
//鑰冭檻鐗規(guī)畩鎯呭喌
if ( f[x][y] != -1 )
return f[x][y];
if ( y == 0 )
return ( f[x][y]
= 0 );
if ( x == 0 )
return ( f[x][y]
= INT_MAX );
int i;
__int64 temp;
f[x][y] = INT_MAX;
for (i=x-1; i<y; i++)
{
//絎?/span>x涓漢澶嶅埗絎?/span>i+1鍒扮y鏈功涓庡墠x-1涓漢澶嶅埗鍓?/span>i鏈功鐨勬椂闂磋緝澶х殑鏃墮棿
temp = Max(
Min(x-1, i), Sum(i+1, y) );
if ( temp <
f[x][y] )
{
f[x][y]
= temp;
}
}
return f[x][y];
}
void Output
()
{
int i, p;
__int64 temp;
int slash[MAX];
max = f[k][m];
memset(slash, 0, sizeof(slash));
temp = 0;
p = k;
for (i=m; i>0; i--)
{
//璁╁悗闈㈢殑鍛樺伐灝介噺澶嶅嵃鏈澶氱殑涔︾睄
if ( temp +
book[i] > max || i < p )
{
slash[i]
= 1;
temp
= book[i];
p
--;
}
else
{
temp
+= book[i];
}
}
for (i=1; i<=m; i++)
{
printf("%d",
book[i]);
if ( slash[i] ==
1 )
printf("
/ ");
else if ( i != m
)
printf("
");
}
printf("\n");
}
void Solve
()
{
int i, j;
//棰勫鐞嗕功綾嶉〉鏁扮殑鍜?/span>
total[0] = 0;
for (i=1; i<=m; i++)
total[i] =
total[i-1] + book[i];
memset(f, -1, sizeof(f));
Min(k, m);
Output();
}
int main ()
{
int test;
scanf("%d",
&test);
while ( test-- )
{
Input ();
Solve ();
}
return 0;
}
紼嬪簭鍒嗘瀽涓庡績(jī)寰楋細(xì)
鏃墮棿澶嶆潅搴?/span>O(n2)錛岀┖闂村鏉傚害O(n2)銆?/span>
鍦ㄧ敤璁板繂鍖栨悳绱㈣В鍐?/span>DP闂鏃訛紝寰寰姣旇緝絎﹀悎浜虹殑鎬濈淮錛屽鏄撴兂鍒版ā鍨嬶紝緙栫▼姣旇緝綆鍗曘傚湪瑙i榪囩▼涓紝闄や簡(jiǎn)鍙互鎸夌収甯哥悊欏虹潃鎺紝涔熷彲浠ュ皾璇曢嗗悜鎬濈淮錛屼粠鏈鍚庣殑鐘舵佸掔潃鎺紝榪欐牱鍙互浣塊棶棰樻兂寰楁洿鍔犻忓交錛屾湁姣旇緝濂界殑鏁堟灉銆?/span>
Source Code
Problem: 2907 | User: QuXiao | |
Memory: 176K | Time: 0MS | |
Language: C++ | Result: Accepted |
#include <iostream>
#include <climits>
using namespace std;
struct Point
{
int x, y;
};
int num, X, Y;
Point start, beeper[15];
int shortest;
int visited[15];
int Length (Point p1, Point p2)
{
return abs(p1.x - p2.x) + abs(p1.y - p2.y);
}
void Input ()
{
int i;
cin>>X>>Y;
cin>>start.x>>start.y;
cin>>num;
for (i=0; i<num; i++)
cin>>beeper[i].x>>beeper[i].y;
}
void DFS (int cur, int len, int n)
{
if ( n == num )
{
int t = Length(beeper[cur], start);
if ( len + t < shortest )
shortest = len + t;
}
else if ( len < shortest )
{
int i;
for (i=0; i<num; i++)
{
if ( visited[i] == 0 )
{
visited[i] = 1;
DFS (i, len+Length(beeper[cur], beeper[i]), n+1);
visited[i] = 0;
}
}
}
}
void Solve ()
{
int i, t;
shortest = INT_MAX;
memset(visited, 0, sizeof(visited));
for (i=0; i<num; i++)
{
t = Length(beeper[i], start);
visited[i] = 1;
DFS (i, t, 1);
visited[i] = 0;
}
cout<<"The shortest path has length "<<shortest<<endl;
}
int main ()
{
int test;
cin>>test;
while ( test-- )
{
Input ();
Solve ();
}
return 0;
}