2013-06-11 12:30:55Morris

[UVA] 10354 - Avoiding Your Boss

Problem B
Avoiding Your Boss
Input: standard input
Output: standard output
Time Limit: 4 seconds
Memory Limit: 32 MB

 

Imagine that like me you are also an idle but reasonably good programmer. On one fine morning you wake up from sleep and discover that your bed is such a lovely place and you must not leave it for the miserable place called office. So you just pick up your mobile phone, call your boss with a gloomy voice and tell him that you are quite sick. Your boss is a very strict but sympathetic person and so he grants you leave via phone. As the day grows old you discover that you must go out for shopping. But you are afraid that your boss might see you. So you need to know if there is any safe option to reach the market. You know that your boss stays only in two places, his residence and the office. While going from one place to another he uses a path that does not cost more than any other path. So you must avoid such paths and even places that your boss may reach. Your boss must not see you outside your home. You can assume that the cost of reaching another location in the same place is zero and you must go outside your home to reach market and your boss must come outside to reach home (from office) or office (from home). As your job is at stake so you cannot take any chances.

 

Input

The input file contains several sets of input. Each set of input starts with six integers P, R, BH, OF, YH and M. Here

 

                P = Total number of places. (0<P<=100)

                R = Total number of connecting roads. (0<=R<=4950)

                BH = The Place where your boss lives. (0<BH<=P)

                OF = The place where your office is situated. (0<OF<=P)

                YH = The place where your home is situated. (0<YH<=P)

                M = The place where the market is situated. (0<M<=P)

               

Next R lines contain description of the city. Each line contains three integers p1, p2 and d. These three integers denote that place p1 and place p2 is connected by a road whose cost is d. Here (0<p1, p2<=P) and d is a positive integer less than 101. Streets are all bi-directional. You can assume that two different places are connected by only one road. Input is terminated by end of file.

 

Output

For each set of input produce one line of output. This line contains the cost of going from your home to the market. If it is not possible for you to go to the market avoiding your boss print the line MISSION IMPOSSIBLE.’ (Without the quotes).

 

Sample Input
3 2 2 3 1 3

1 2 4

2 3 4

3 2 2 3 3 3

1 2 4

2 3 4

4 3 2 3 1 4

1 2 4

2 3 4

1 4 10


Sample Output
MISSION IMPOSSIBLE.

MISSION IMPOSSIBLE.

10


Shahriar Manzoor

題目描述:

老闆會在他的住所與辦公室的最短路上,身為員工想要從家裡去市場,但不能遇到老闆的最短路為何?

題目解法:

由於老闆會經過在最短路上的所有點(可能有數條最短路)

對於這些點被標記不可走,然後再做一次最短路即可。

#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
    int P, R, BH, OF, YH, M;
    int i, j, k, x, y, v;
    while(scanf("%d %d %d", &P, &R, &BH) == 3) {
        scanf("%d %d %d", &OF, &YH, &M);
        int g1[105][105], g2[105][105];
        for(i = 0; i <= P; i++) {
            for(j = 0; j <= P; j++)
                g1[i][j] = g2[i][j] = 1e9;
            g1[i][i] = g2[i][i] = 0;
        }
        while(R--) {
            scanf("%d %d %d", &x, &y, &v);
            g1[x][y] = g1[y][x] = v;
            g2[x][y] = g2[y][x] = v;
        }
        for(k = 1; k <= P; k++)
            for(i = 1; i <= P; i++)
                for(j = 1; j <= P; j++)
                    g1[i][j] = min(g1[i][j], g1[i][k]+g1[k][j]);
        int ban[105] = {};
        for(k = 1; k <= P; k++)
            if(g1[BH][OF] == g1[BH][k] + g1[k][OF])
                ban[k] = 1;
        for(k = 1; k <= P; k++) {
            if(ban[k] == 0)
            for(i = 1; i <= P; i++) {
                if(ban[i] == 0)
                for(j = 1; j <= P; j++) {
                    if(ban[j] == 0)
                        g2[i][j] = min(g2[i][j], g2[i][k]+g2[k][j]);
                }
            }
        }
        if(g2[YH][M] != 1e9 && ban[YH] == 0 && ban[M] == 0)
            printf("%d\n", g2[YH][M]);
        else
            puts("MISSION IMPOSSIBLE.");
    }
    return 0;
}