UVA 10160 Servicing Stations

来源:互联网 发布:调酒师动漫知乎 编辑:程序博客网 时间:2024/03/29 20:05

分析

给定一些城镇和相邻关系,如果要建造服务站,服务站只能服务建造地所在的城镇和其相邻的城镇,问最少要建几个服务站?

对每一个城镇,判断是否需要在此处建造服务站,即相邻的城镇是否还需要建站。如果不需要则此处不建站(如该镇自身未建站则需建站)。继续判断下一个城镇。这里进行了一定剪枝,如果当前城镇之前的城镇未能享受服务,并且其相邻的最大城镇小于当前城镇,即不可能再使之前那个城镇享受到服务,所以这种安排是不可以的。

代码

#include <cstdio>#include <cstring>#include <algorithm>#define MAX_V 40using std::sort;int V, E, min;int edge[MAX_V][MAX_V], top[MAX_V], vis[MAX_V];void dfs(int p, int n, int s){    if (n == V && s < min) { min = s; return; }    if (p > V || s >= min) return;    for (int i = 1; i < p; i++)        if (!vis[i] && edge[i][top[i]-1] < p) return;    int r = 0;    for (int i = 0; i < top[p]; i++) {        if (!vis[edge[p][i]]) r++;        vis[edge[p][i]]++;    }    if (r) dfs(p + 1, n + r, s + 1);    for (int i = 0; i < top[p]; i++)        vis[edge[p][i]]--;    dfs(p + 1, n, s);}void solve(){    for (int i = 1; i <= V; i++) {        edge[i][top[i]++] = i;        sort(edge[i], edge[i]+top[i]);    }    min = V;    dfs(1, 0, 0);    printf("%d\n", min);}int main(){    while (scanf("%d%d", &V, &E), (V + E)) {        memset(edge, 0, sizeof(edge));        memset(top, 0, sizeof(top));        memset(vis, 0, sizeof(vis));        int v1, v2;        for (int i = 0; i < E; i++) {            scanf("%d%d", &v1, &v2);            edge[v1][top[v1]++] = v2;            edge[v2][top[v2]++] = v1;        }        solve();    }    return 0;}

题目

Description

A company offers personal computers for sale in N towns (3 ≤ N ≤ 35). The towns are denoted by 1, 2, … , N. There are direct routes connecting M pairs from among these towns. The company decides to build servicing stations in several towns, so that for any town X, there would be a station located either in X or in some immediately neighbouring town of X.

Write a program for finding out the minumum number of stations, which the company has to build, so that the above condition holds.

Input

The input consists of more than one description of town (but totally, less than ten descriptions). Every description starts with number N of towns and number M of pairs of towns directly connected each other. The integers N and M are separated by a space. Every one of the next M rows contains a pair of connected towns, one pair per row. The pair consists of two integers for town’s numbers, separated
by a space. The input ends with N = 0 and M = 0.

Output

For every town in the input write a line containing the obtained minimum.

Sample Input

8 121 21 61 82 32 63 43 54 54 75 66 76 812 5112 107 122 81 129 1211 311 74 123 42 35 32 510 17 21 66 56 84 118 73 711 211 93 109 58 98 104 107 65 81 54 74 64 52 102 46 1111 1212 88 311 1010 57 99 62 67 51 24 83 910 76 31 825 25124 2522 1719 209 2325 109 175 151 1220 164 1512 1111 2315 1021 2415 2010 1323 224 325 2118 1422 511 1415 1811 154 815 2313 14 137 1618 2425 66 1217 419 222 2016 217 19 1910 116 1724 512 2224 1720 1315 76 2123 2419 2116 1921 1425 233 2023 138 157 62 1911 193 2521 239 127 136 1112 814 920 73 1724 612 35 75 2125 913 1618 2515 122 52 174 204 77 1713 2421 217 108 710 1817 1214 725 47 252 249 2110 621 1111 918 1219 39 2015 225 1122 2114 198 108 1720 102 16 818 176 322 36 518 318 924 1923 1219 1720 1810 2422 243 717 2318 232 317 1122 624 1122 166 155 96 235 164 119 1521 1011 76 139 1511 54 2416 611 114 205 1317 1519 64 910 141 175 128 2116 1518 416 2515 2119 1811 32 1119 1223 195 196 1720 118 2214 251 259 2417 1412 2410 228 2210 925 1222 1320 623 2013 83 1622 114 515 142 2218 24 213 182 163 1511 82 1017 258 54 2223 420 2116 823 1022 2512 1425 1522 111 1913 311 101 68 145 2310 1224 86 1418 2113 1222 2015 222 63 816 1814 132 811 1613 113 2116 1023 37 243 2416 1223 1625 1913 191 182 2320 247 1213 1524 125 1315 2414 2414 12 1419 44 1021 77 221 48 1811 1825 2017 2018 74 118 1913 216 2418 559 1417 714 612 73 711 37 154 1514 25 613 914 412 216 43 613 85 107 516 134 1110 179 119 610 111 511 66 1810 36 1218 154 179 74 37 1416 175 313 61 102 36 412 412 914 1013 1711 141 1216 316 62 918 1313 26 1716 915 1217 527 3085 229 2416 1925 2423 210 165 1522 1927 219 196 1516 113 1515 1927 149 2313 1219 255 2314 62 1321 2010 131 423 2710 1515 16 2517 2325 108 1826 2220 711 1926 725 323 1326 1827 2518 105 419 414 1924 1625 235 2112 1015 2712 54 104 2017 1618 1515 2524 313 1526 617 1225 84 86 85 1123 321 420 116 78 320 198 1526 1412 1113 1824 1114 517 2714 326 168 1920 34 187 187 416 1321 1622 165 164 324 51 1624 186 11 215 2019 715 25 2527 827 129 2126 1026 1716 2321 717 1426 523 1013 1910 51 1822 202 116 1713 114 157 1714 21 1220 241 923 123 191 1023 1124 2217 1818 1920 1822 1325 127 1818 24 918 2112 2417 2125 924 414 237 84 2519 2724 616 2711 67 2212 2521 1216 1422 1422 614 215 2020 618 512 202 1011 816 626 2716 2519 222 816 183 132 75 11 1911 33 1012 924 179 1111 1426 1526 138 517 93 2113 726 2117 113 141 37 93 1614 2513 419 39 147 235 327 521 1012 1620 1426 2311 74 2321 811 2513 2117 215 232 2017 2018 315 922 2519 2619 1712 1812 23 2210 1922 2121 255 719 1219 611 212 325 2624 142 2610 1113 2010 1713 258 1715 127 2525 184 1414 76 91 76 1027 118 236 224 22 2227 93 2615 163 129 516 924 126 129 31 1112 2220 1110 2722 1813 615 2213 177 2427 726 247 1524 812 66 2119 2119 523 1225 209 1315 216 54 265 210 927 68 1025 29 29 187 64 610 2024 152 822 919 2416 813 111 171 2118 1117 2523 616 2011 265 1327 211 2227 2016 222 2724 2310 2226 2014 1012 84 52 41 43 23 14 323 1007 1218 121 222 39 1921 816 65 1910 314 49 711 123 2123 168 122 62 166 112 2215 223 41 1621 214 185 911 219 33 238 1423 194 1713 1423 94 94 1512 1420 105 1322 68 1719 124 1120 91 1115 1722 717 223 1814 510 214 615 217 1011 1919 712 152 1013 2311 517 613 320 1617 1922 178 65 1218 52 2019 89 823 214 316 513 75 2013 184 2022 189 118 168 2313 1917 237 1818 1716 719 1616 94 818 210 818 1114 157 16 1314 1010 198 2217 114 2228 26018 1413 119 55 1915 68 914 214 135 2328 2517 1418 2711 216 2025 1623 228 2213 227 138 2720 1218 2422 522 253 1312 267 267 1910 2627 2522 134 322 617 713 238 1412 1310 1515 1826 1621 163 1815 88 117 2121 2622 316 1518 624 2019 1526 2722 425 1012 247 219 163 724 77 286 82 243 1216 823 1018 253 25 2512 66 2428 1014 2424 1312 109 2317 827 715 313 726 1821 2522 73 1415 2212 198 184 167 619 47 1525 822 2617 2722 121 2820 2212 1710 1715 124 216 518 2019 39 221 26 1320 114 920 114 279 1018 94 238 122 122 275 113 1910 2722 825 628 2711 198 133 2716 129 126 149 225 2113 48 727 926 414 1525 13 811 177 251 223 614 1023 1428 2324 2224 2611 245 1117 1528 628 1610 222 1120 54 82 181 1323 1225 1217 513 2123 223 54 2725 227 2327 121 417 2019 231 1914 2225 2315 284 2811 212 65 1511 2614 916 521 1721 1013 1024 516 1716 1113 1514 1910 1819 821 277 1424 2110 1920 2328 2018 522 1711 2319 2716 99 256 924 1611 610 522 2715 920 142 2226 84 723 1810 2424 2810 2224 2518 174 2516 1418 192 2820 219 2624 1719 263 1610 163 614 2111 79 2118 214 1211 1217 2514 2614 2820 725 318 1319 218 59 76 1626 28 1211 1526 281 611 102 1914 18 2827 1612 2819 2018 1219 2423 3323 1314 136 2221 178 1817 813 227 221 109 2315 818 113 172 613 1715 2020 186 916 1221 153 106 107 1915 57 1017 922 720 1417 161 136 1913 1116 1314 745 32 59 611 1010 613 713 512 138 36 712 510 47 22 84 112 109 113 414 18 92 49 1310 133 116 37 106 1314 1014 55 711 138 132 114 713 142 93 128 1010 1210 312 46 19 1411 1412 71 514 21 811 811 72 139 32 64 610 511 28 126 53 1310 15 41 135 98 1411 14 136 114 17 31 99 1011 1212 212 623 20711 1019 1213 1223 1518 77 1922 236 1017 2316 1018 1715 173 99 1120 64 1714 2218 220 185 135 1720 1522 165 713 228 144 116 125 154 2318 613 2015 318 2111 1222 172 420 1112 224 620 2321 126 512 111 225 117 1020 227 131 1412 713 38 223 2121 165 24 2214 1919 183 1012 1019 47 46 1916 29 1020 1715 25 2323 105 1217 1619 1322 1015 43 1112 37 217 818 102 105 83 812 157 1523 2111 29 1917 144 135 1623 1317 111 310 1520 1616 73 1716 1212 2315 193 621 2020 223 1421 82 2111 2113 623 17 227 1415 921 2219 118 107 619 229 610 1319 1020 320 821 196 212 2316 139 813 2117 136 231 1822 223 88 1620 105 45 142 1718 1415 87 33 1813 1418 817 64 1216 238 12 920 1423 315 139 221 62 143 1918 1514 1620 911 187 27 918 1315 2116 316 1811 81 116 1617 197 202 110 412 148 220 1911 76 84 1818 1210 518 51 163 48 1917 715 2219 24 81 199 1810 2114 31 105 215 98 1312 83 22 62 1212 209 1420 42 1317 817 3314 154 811 616 46 1017 1310 1315 57 613 81 127 1711 1316 516 115 1212 138 58 613 37 144 214 1017 141 411 1210 1713 517 1211 815 134 1113 1632 13218 2720 2519 91 138 3016 2422 83 2730 2517 1817 208 269 126 315 106 283 222 212 2527 1031 2129 1023 3022 1929 72 3028 2515 239 41 237 2414 2820 1020 2328 2112 24 1429 330 2112 2628 728 2915 3221 172 2916 1519 1514 2729 206 2624 61 87 1529 1918 159 248 2912 1023 411 411 14 218 95 825 2930 2710 1916 199 1516 2820 3130 2930 245 1323 719 2527 3131 2927 1618 2110 167 214 309 818 3117 2217 1330 3214 2225 187 917 104 1731 326 113 2027 2526 1022 611 1821 136 1912 810 1820 82 627 628 917 2416 2530 1522 263 3130 321 2623 34 1222 2523 2223 1417 77 1119 2420 1429 424 1228 2719 1817 32 1822 2815 1027 916 259 39 159 58 2427 2412 2011 1715 103 2210 2611 242 1027 75 1526 1922 52 2419 424 46 1521 1313 275 1019 202 219 219 326 1518 314 36 1418 1715 135 1414 910 275 225 49 14 1720 2526 318 1019 116 1323 1926 2010 1116 718 2217 1321 1526 139 219 422 48 132 64 153 1012 1113 1022 209 254 77 263 2327 2326 516 526 1125 2717 246 189 192 113 2313 2220 327 1626 272 811 181 2220 1017 192 164 1627 314 1719 2810 76 1119 912 1413 23 147 215 416 147 146 157 93 1812 196 1610 176 133 28 159 1413 1711 510 38 127 45 1416 1819 225 3525 216 523 1313 93 1512 242 168 215 2219 2218 15 83 1315 722 1119 184 1424 524 617 222 1214 2513 1713 621 1719 316 2112 1419 106 175 209 1617 815 128 1018 327 134 1816 917 26 22 1314 39 81 1718 106 174 714 1815 149 51 1515 113 915 174 1518 33 516 516 43 25 710 1412 212 613 49 175 13 22 31 20 032 596 929 2524 3132 35 2214 1130 2622 3112 2130 427 1417 88 1927 118 330 220 1720 1616 95 424 1531 2819 304 1610 2131 614 3020 249 2426 1825 1424 2816 227 312 515 2232 222 1731 323 924 2313 727 2923 1927 105 323 131 219 2726 1428 2530 1617 2830 315 615 2128 2618 3021 3021 9919 2013 179 1917 36 913 1012 221 2011 1818 221 1018 144 63 620 1313 1117 219 320 116 141 137 141 216 102 914 1119 66 719 159 79 513 714 154 919 315 512 317 515 109 152 1516 158 1919 1210 1412 1513 1820 713 95 145 320 32 71 913 1211 56 1417 1210 910 216 418 610 519 184 318 2014 1719 102 2018 34 129 1411 2111 1521 138 2014 2021 815 2019 1416 612 58 1112 917 713 62 1318 165 62 88 178 16 1217 47 1210 82 518 1519 210 0

Sample Output

221311327114557851
0 0
原创粉丝点击