File size: 1,032 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
  >>~~ UVa Online Judge ACM Problem Solution ~~<<

  ID: 10000
  Name: Longest Paths
  Problem: https://onlinejudge.org/external/100/10000.pdf
  Language: C++

  Author: Arash Shakery
  Email: arash.shakery@gmail.com
*/

#include <stdio.h>
#include <iostream>
using namespace std;

int main(){
    int cse = 1;
    int n, s, ev[110*110], ew[110*110], d[100];
    while (cin>>n && n) {
        cin>>s;

        int en;
        for (en=0; cin>>ev[en]>>ew[en]; en++)
            if (!ev[en] || !ew[en])
                break;

        for (int i=0; i<=n; i++)
            d[i] = 1000000;
        d[s] = 0;

        for (int i=0; i<n; i++)
            for (int i=0; i<en; i++)
                if (d[ev[i]] - 1 < d[ew[i]])
                    d[ew[i]] = d[ev[i]] - 1;

        int v=s, vl=0;
        for (int i=1; i<=n; i++)
            if (d[i] < vl) {
                v = i;
                vl = d[i];
            }

        printf("Case %d: The longest path from %d has length %d, finishing at %d.\n\n", cse++, s, -vl, v);
    }
}