【UVa 208】Firetruck

The Center City ?re department collaborates with the transportation department to maintain maps
of the city which re?ects the current status of the city streets. On any given day, several streets are
closed for repairs or construction. Fire?ghters need to be able to select routes from the ?restations to
?res that do not use closed streets.
Central City is divided into non-overlapping ?re districts, each containing a single ?restation. When
a ?re is reported, a central dispatcher alerts the ?restation of the district where the ?re is located and
gives a list of possible routes from the ?restation to the ?re. You must write a program that the central
dispatcher can use to generate routes from the district ?restations to the ?res.
Input
The city has a separate map for each ?re district. Streetcorners of each map are identi?ed by positive
integers less than 21, with the ?restation always on corner #1. The input ?le contains several test cases
representing di?erent ?res in di?erent districts.
• The ?rst line of a test case consists of a single integer which is the number of the streetcorner
closest to the ?re.
• The next several lines consist of pairs of positive integers separated by blanks which are the
adjacent streetcorners of open streets. (For example, if the pair 4 7 is on a line in the ?le, then
the street between streetcorners 4 and 7 is open. There are no other streetcorners between 4 and
7 on that section of the street.)
• The ?nal line of each test case consists of a pair of 0’s.
Output
For each test case, your output must identify the case by number (‘CASE 1:’, ‘CASE 2:’, etc). It must
list each route on a separate line, with the streetcorners written in the order in which they appear on
the route. And it must give the total number routes from ?restation to the ?re. Include only routes
which do not pass through any streetcorner more than once. (For obvious reasons, the ?re
department doesn’t want its trucks driving around in circles.)
Output from separate cases must appear on separate lines.
Sample Input
6
1 2
1 3
3 4
3 5
4 6
5 6
2 3
2 4
0 0
4
2 3
3 4
5 1
1 6
7 8
8 9
2 5
5 7
3 1
1 8
4 6
6 9
0 0
Sample Output
CASE 1:
1 2 3 4 6
1 2 3 5 6
1 2 4 3 5 6
1 2 4 6
1 3 2 4 6
1 3 4 6
1 3 5 6
There are 7 routes from the firestation to streetcorner 6.
CASE 2:
1 3 2 5 7 8 9 6 4
1 3 4
1 5 2 3 4
1 5 7 8 9 6 4
1 6 4
1 6 9 8 7 5 2 3 4
1 8 7 5 2 3 4
1 8 9 6 4
There are 8 routes from the firestation to streetcorner 4.

深搜即可,无需多讲。

要注意的是,要先判断起点是否和终点在一起(即在一个连通分量中)。否则会超时。这里用了传递闭包。网上说可以用并查集、TARJAN什么的。。。。

#include<cstdio>
#include<cstring>

using namespace std;

int k, path[25], len, tot;
bool v[25], g[25][25];

void solve(int x)
{
    v[x] = false;
    path[len++] = x;
    if (x == k)
    {
        tot++;
        for (int i = 0; i < len; ++i)
        {
            if (i) printf(" ");
            printf("%d", path[i]);
        }
        printf("\n");
        v[x] = true;
        len--;
        return;
    }
    for (int i = 1; i <= 21; ++i)
        if (g[x][i] && v[i])
            solve(i);
    v[x] = true;
    len--;
}

bool it_can_work()
{
    bool dis[25][25];
    memcpy(dis, g, sizeof(g));
    for (int m = 1; m <= 21; ++m)
        for (int i = 1; i <= 21; ++i)
            for (int j = 1; j <= 21; ++j)
                if (i != m && i != j && j != m)
                    dis[i][j] = dis[i][j] || (dis[i][m] && dis[m][j]);
    return dis[1][k];
}

int main()
{
    int kase = 0;
    while (scanf("%d", &k) == 1)
    {
        printf("CASE %d:\n", ++kase);
        int x, y;
        memset(v, true, sizeof(v));
        memset(g, false, sizeof(g));
        while (scanf("%d%d", &x, &y) == 2 && x && y)
            g[x][y] = g[y][x] = true;
        len = tot = 0;
        if (it_can_work())solve(1);
        printf("There are %d routes from the firestation to streetcorner %d.\n", tot, k);
    }
    return 0;
}
时间: 2024-07-28 20:27:55

【UVa 208】Firetruck的相关文章

【uva 658】It&#39;s not a Bug, it&#39;s a Feature!(图论--Dijkstra算法+二进制表示)

题意:有n个潜在的bug和m个补丁,每个补丁用长为n的字符串表示.首先输入bug数目以及补丁数目.然后就是对m 个补丁的描述,共有m行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,地一个字符串 是对软件的描述,只有软件处于该状态下才能打该补丁该字符串的每一个位置代表bug状态(-代表该位置没bug,+代 表该位置有bug,0表示该位置无论有没有bug都可打补丁).然后第二个字符串是对打上补丁后软件状态的描述 -代表该位置上的bug已经被修复,+表示该位置又引入了一个新的bug

【UVa 10881】Piotr&#39;s Ants

Piotr's Ants Porsition:Uva 10881 白书P9 中文改编题:[T^T][FJUT]第二届新生赛真S题地震了 "One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one, welcome our new insect overlords."Kent Brockman Piotr likes playing with ants. H

【UVa 10815】Andy&#39;s First Dictionary

真的只用set和string就行了. 如果使用PASCAL的同学可能就要写个treap什么的了,还要用ansistring. #include<cstdio> #include<cstring> #include<iostream> #include<string> #include<set> using namespace std; string s; string now; set<string> dict; bool is_al

【UVA - 10815】Andy&#39;s First Dictionary (set)

Andy's First Dictionary Description 不提英文了 直接上中文大意吧 XY学长刚刚立下了再不过CET就直播xx的flag,为了不真的开启直播模式,XY学长决定好好学习英语.于是他每天都读一篇只包含生词的英语文章,并以自己高达450的智商在一秒钟之内记忆下来. 现在给你一篇XY学长今天要读的文章,请你写一个程序,输出他都学习到了哪些单词.要求:如果文章中有相同的单词,那么仅仅输出一次:而且如果两个单词只有大小写不同,将他们视为相同的单词. Input 测试数据将输入

【UVA 1395】 Slim Span (苗条树)

[题意] 求一颗生成树,满足最大边和最小边之差最小 InputThe input consists of multiple datasets, followed by a line containing two zeros separated by a space.Each dataset has the following format.n ma1 b1 w1...am bm wmEvery input item in a dataset is a non-negative integer.

【UVA 1151】 Buy or Build (有某些特别的东东的最小生成树)

[题意] 平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此,你可以新建一些边,费用等于两个端点的欧几里得距离的平方. 另外还有q(0<=q<=8)个套餐,可以购买,如果你购买了第i个套餐,该套餐中的所有结点将变得相互连通,第i个套餐的花费为ci. 求最小花费. Input (1 ≤ n ≤ 1000)  (0 ≤ q ≤ 8). The second integer is the the cost of the subnetwork(not greater

【UVA 10369】 Arctic Network (最小生成树)

[题意] 南极有n个科研站, 要把这些站用卫星或者无线电连接起来,使得任意两个都能直接或者间接相连.任意两个都有安装卫星设备的,都可以直接通过卫星通信,不管它们距离有多远. 而安装有无线电设备的两个站,距离不能超过D. D越长费用越多. 现在有s个卫星设备可以安装,还有足够多的无线电设备,求一个方案,使得费用D最少(D取决与所有用无线电通信的花费最大的那条路径). InputThe first line of input contains N, the number of test cases.

【uva 10954】Add All(算法效率+Huffman编码+优先队列)

题意:有N个数,每次选2个数合并为1个数,操作的开销就是这个新的数.直到只剩下1个数,问最小总开销. 解法:合并的操作可以转化为二叉树上的操作[建模],每次选两棵根树合并成一棵新树,新树的根权值等于两棵合并前树的根权值和(也与Huffman编码的建立过程类似,选权值最小的两棵树). 这样总开销就是除了叶子结点的权值和  => 每个叶子结点的权值*层数(根节点层数为0)之和  => WPL(树的所有叶子节点的带权路径长度之和,即该节点到根节点路径长度与节点上权的乘积之和). 而Huffman树就

【uva 10570】Meeting with Aliens(算法效率--暴力+贪心)

题意:输入1~N的一个排列,每次可以交换2个整数,问使排列变成1~N的一个环状排列所需的虽少交换次数.(3≤N≤500) 解法:(又是一道我没打代码,光想和看就花了很久时间的题~QwQ)由于n很小,可以暴力枚举目标的环状排列,于是贪心交换——把元素 x 直接与它的目标位置上的元素互换,这样至少使1个元素的位置正确了.而若 x 先与其他 k 个元素交换,是最多能得到 k+1 个元素的正确排列的,这样并没有之前的策略优.    另外,网上关于此题还有一种关于对链状序列找环的说法,我更加不理解.若有人