[UVA][中路] 10296 - Jogging Trails@Morris' Blog|PChome Online 人新台
2012-04-28 17:34:50| 人2,010| 回0 | 上一篇 | 下一篇

[UVA][中路] 10296 - Jogging Trails

0 收藏 0 0 站台

Problem B: Jogging Trails

Gord is training for a marathon. Behind his house is a park with a large network of jogging trails connecting water stations. Gord wants to find the shortest jogging route that travels along every trail at least once.

Input consists of several test cases. The first line of input for each case contains two positive integers: n <= 15, the number of water stations, and m < 1000, the number of trails. For each trail, there is one subsequent line of input containing three positive integers: the first two, between 1 and n, indicating the water stations at the end points of the trail; the third indicates the length of the trail, in cubits. There may be more than one trail between any two stations; each different trail is given only once in the input; each trail can be travelled in either direction. It is possible to reach any trail from any other trail by visiting a sequence of water stations connected by trails. Gord's route may start at any water station, and must end at the same station. A single line containing 0 follows the last test case.

For each case, there should be one line of output giving the length of Gord's jogging route.

Sample Input

4 5 1 2 3 2 3 4 3 4 5 1 4 10 1 3 12 0 

Output for Sample Input

41


利用尤拉道的概念, 每都必有偶 deg,
在此中, 如果生奇 deg 的, 其匹配, 使其成尤拉道,
其匹配的成本越小越好
#include <stdio.h>
#include <string.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
int map[16][16], odd[16];
void floyd(int n) {
int i, j, k;
for(k = 1; k <= n; k++)
for(i = 1; i <= n; i++)
for(j = 1; j <= n; j++)
map[i][j] = min(map[i][j], map[i][k]+map[k][j]);
}
int dp[1<<16];
int build(int pN, int ot) {
if(pN == 0)
return 0;
if(dp[pN] != -1)
return dp[pN];
int i, j, tmp;
dp[pN] = 0xfffffff;
for(i = 0; i < ot; i++) {
if(pN&(1<<i)) {
for(j = i+1; j < ot; j++) {
if(pN&(1<<j)) {
tmp = build(pN-(1<<i)-(1<<j), ot);
dp[pN] = min(dp[pN], tmp+map[odd[i]][odd[j]]);
}
}
break;
}
}
return dp[pN];
}
int main() {
int n, m, x, y, w;
while(scanf("%d", &n) == 1 && n) {
scanf("%d", &m);
memset(map, 63, sizeof(map));
memset(dp, -1, sizeof(dp));
int sum = 0, deg[16] = {};
while(m--) {
scanf("%d %d %d", &x, &y, &w);
map[x][y] = min(map[x][y], w);
map[y][x] = min(map[y][x], w);
deg[x]++, deg[y]++;
sum += w;
}
floyd(n);
int ot = 0;
for(int i = 1; i <= n; i++)
if(deg[i]&1)
odd[ot++] = i;
printf("%d\n", sum+build((1<<ot)-1, ot));
}
return 0;
}
 

台: Morris
人(2,010) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][文] 11309 - Counting Chaos
此分上一篇:[UVA][Math] 11152 - Colourful Flowers

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86