最短路 (迪杰斯特拉算法)



poj 1502  MPI Maelstrom http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97858#problem/B 

BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee's research advisor, Jack Swigert, has asked her to benchmark the new system.
       ``Since the Apollo is a distributed shared memory machine, memory access and communication times are not uniform,'' Valentine told Swigert. ``Communication is fast between processors that share the same memory subsystem, but it is slower between processors that are not on the same subsystem. Communication between the Apollo and machines in our lab is slower yet.''
       ``How is Apollo's port of the Message Passing Interface (MPI) working out?'' Swigert asked.
       ``Not so well,'' Valentine replied. ``To do a broadcast of a message from one processor to all the other n-1 processors, they just do a sequence of n-1 sends. That really serializes things and kills the performance.''
       ``Is there anything you can do to fix that?''
       ``Yes,'' smiled Valentine. ``There is. Once the first processor has sent the message to another, those two can then send messages to two other hosts at the same time. Then there will be four hosts that can send, and so on.''
       ``Ah, so you can do the broadcast as a binary tree!''
       ``Not really a binary tree -- there are some particular features of our network that we should exploit. The interface cards we have allow each processor to simultaneously send messages to any number of the other processors connected to it. However, the messages don't necessarily arrive at the destinations at the same time -- there is a communication cost involved. In general, we need to take into account the communication costs for each link in our network topologies and plan accordingly to minimize the total time required to do a broadcast.''
     
  Input The input will describe the topology of a network connecting n processors. The first line of the input will be n, the number of processors, such that 1 <= n <= 100.
       The rest of the input defines an adjacency matrix, A. The adjacency matrix is square and of size n x n. Each of its entries will be either an integer or the character x. The value of A(i,j) indicates the expense of sending a message directly from node i to node j. A value of x for A(i,j) indicates that a message cannot be sent directly from node i to node j.
       Note that for a node to send a message to itself does not require network communication, so A(i,i) = 0 for 1 <= i <= n. Also, you may assume that the network is undirected (messages can go in either direction with equal overhead), so that A(i,j) = A(j,i). Thus only the entries on the (strictly) lower triangular portion of A will be supplied.
       The input to your program will be the lower triangular section of A. That is, the second line of input will contain one entry, A(2,1). The next line will contain two entries, A(3,1) and A(3,2), and so on.
     
  Output Your program should output the minimum communication time required to broadcast a message from the first processor to all the other processors.
     
  Sample Input

5
50
30 5
100 20 50
10 x x 10
 Sample Output

35

 AC 代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

bool used[1005];
int gra[1005][1005];
int dist[1005];
int n;

void diks(int s){
    memset(used,false,sizeof(used));
    for(int i = 1;i <= n;i++){
        dist[i] = 999999;
    }
    dist[s] = 0;

    while(1){
        int tag = 0;
        int v;
        int mai = 999999;
        for(int i = 1;i <= n;i++){
            if(!used[i] && dist[i] < mai){
                mai = dist[i];
                v = i;
                tag = 1;
            }
        }
        if(!tag){
            break;
        }
        used[v] = true;
        for(int i = 1;i <= n;i++){
            if(!used[i] && dist[i] > dist[v] + gra[v][i]){
                dist[i] = dist[v] + gra[v][i];
            }
        }

    }
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {

        for(int i=2; i<=n; i++)
        {
            for(int j=1; j<i; j++)
            {
                int t;
                gra[i][j]=99999999;
                gra[j][i]=99999999;
                if(scanf("%d",&t))
                    gra[i][j]=gra[j][i]=t;
                else
                    scanf("x");
            }
        }

        int sum = 0;
        diks(1);
        for(int i = 1;i <= n;i++){
            sum  = max(sum,dist[i]);
        }
        printf("%d\n",sum);
    }

  return 0;
}


POJ  2387 Til the Cows Come Home http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97858#problem/D 

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
       Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
       Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
     
  Input
       * Line 1: Two integers: T and N * Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
     
  Output
       * Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
      
 Sample Input

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
 Sample Output

90
  Hint INPUT DETAILS:
       There are five landmarks.
       OUTPUT DETAILS:
       Bessie can get home by following trails 4, 3, 2, and 1.


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

int INF =  99999;
int cost[2050][2050];
int d[2050];
bool used[2050];
int V;
int t;

void diskester(int s){
    memset(used,false,sizeof(used));
    d[s] = 0;
    while(true){
        int v = -1;
        for(int u = 0;u < V;u++){
            if(!used[u] && (v == -1 || d[u] < d[v])){
                v = u;
            }
        }
        if(v == -1) break;
        used[v] = true;
        for(int u = 0;u < V;u++){
            d[u] = min(d[u],d[v]+cost[v][u]);
        }
    }
}

int main(){
    while(~scanf("%d%d",&t,&V)){
        if(t == 0 && V == 0){
            break;
        }
       for(int i = 1 ;i <= V;i++){
            for(int j = 1;j <= V;j++){
                cost[i-1][j-1] = INF;
                d[i-1] = INF;
            }
       }
        for(int i = 1;i <= t;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(cost[a-1][b-1] != INF){
                cost[a-1][b-1] = min(cost[a-1][b-1],c);
                cost[b-1][a-1] = cost[a-1][b-1];
            }
            else{
                cost[a-1][b-1] = c;
                cost[b-1][a-1] = cost[a-1][b-1];
            }
        }
        diskester(0);
        printf("%d\n",d[V-1]);
    }
    return 0;
}


POJ 3268 Silver Cow Party http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97858#problem/E 

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
  Input Line 1: Three space-separated integers, respectively:
       N,
       M, and XLines 2..
       M+1: Line i+1 describes road i with three space-separated integers:
       Ai,
       Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
     
  Output Line 1: One integer: the maximum of time any one cow must walk.
     
  Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
 Sample Output10
  Hint Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int N,M,X;
int dist[1005];
int dist2[1005];
int dist3[1005];
bool used[1005];
int gra[1005][1005];
int max1 = 0x7ffffff;


void dik(int s){
    memset(used,false,sizeof(used));
    for(int i = 1; i <= N;i++){
        dist[i] = max1;
    }
    dist[s] = 0;
    while(1){
            int v;
            int min1 = max1;
            int tag = 0;
            for(int i = 1;i <= N;i++){
                if(!used[i] && dist[i] < min1){
                    min1 = dist[i];
                    v = i;
                    tag = 1;
                }
            }
            if(!tag){
                break;
            }
            used[v] = true;
            for(int i = 1; i <= N;i++){
                if(!used[i]){
                    if(dist[v] + gra[v][i] < dist[i]){
                        dist[i] = dist[v] + gra[v][i];
                    }
                }
            }
     }
}

int main(){

    scanf("%d%d%d",&N,&M,&X);
    for(int i = 1; i <= N;i++){
        for(int j = 1;j <= N;j++){
            gra[i][j] = max1;
        }
    }

    int a,b,c;
    while(M--){
        scanf("%d%d%d",&a,&b,&c);
        gra[a][b] = c;
    }

    int ss = 0;
    dik(X);
    for(int i = 1; i <= N;i++){
        dist2[i] = dist[i];
    }
    for(int i = 1; i <= N;i++){
        for(int j = 1;j <= i;j++){
            int t = gra[i][j];
            gra[i][j] = gra[j][i];
            gra[j][i] = t;
        }
    }
    dik(X);
    for(int i = 1; i <= N; i++){
        ss = max(ss,(dist2[i] + dist[i]));
    }
    printf("%d\n",ss);
    return 0;
}


POJ 2502 Subway http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97858#problem/G 

You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school.
       You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
     
  Input Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.
     
  Output Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
      
 Sample Input

0 0 10000 1000
0 200 5000 200 7000 200 -1 -1
2000 600 5000 600 10000 600 -1 -1
Sample Output

21


AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

struct point {
    double x;
    double y;
}p[500];
int n = 2;

double max1 = 0xffffff;
double gra[500][500];
double dist[1005];
bool vis[1005];

void dik(int s){
    for(int i=1;i<=n;i++){
        dist[i]=max1;
        vis[i]=false;
    }
    dist[1] = 0;
    while(1){
        int tag = 0;
        double f = max1;
        for(int i = 1;i <= n;i++){
            if(!vis[i]&&dist[i]<f){
                f = dist[i];
                tag = i;
            }
        }
        if(!tag){
            break;
        }
        vis[tag]=true;
        for(int i = 1;i <= n;i++){
            if(!vis[i] && dist[i] > dist[tag] + gra[tag][i]){
                dist[i] = dist[tag] + gra[tag][i];
            }
        }
    }
}

int main(){

    double r = 10000.0/60;
    double sb = 40000.0/60;

    for(int i = 0;i <= 500;i++){
        for(int j = 0;j <= 500;j++){
            gra[i][j] = max1;
            gra[i][i] = 0;
        }
    }
    scanf("%lf%lf%lf%lf",&p[1].x,&p[1].y,&p[2].x,&p[2].y);
    int cnt = 3;
    double x,y;
    while(~scanf("%lf%lf",&x,&y)){
        if(x == -1 && y == -1){
            cnt = n+1;
            continue;
        }
        n++;
        p[n].x = x;
        p[n].y = y;
        if(n != cnt){
            gra[n][n-1] = gra[n-1][n] = min(gra[n-1][n],sqrt((p[n].x-p[n-1].x)*(p[n].x-p[n-1].x) +(p[n].y-p[n-1].y)*(p[n].y-p[n-1].y))/sb);
        }
    }
    for(int i = 1;i <= n;i++){
        for(int j = 1;j<= n;j++){
            gra[i][j] = min(gra[i][j],sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x) +(p[i].y-p[j].y)*(p[i].y-p[j].y))/r);
        }
    }
    dik(1);
    printf("%.0f\n",dist[2]);
    return 0;
}


你可能感兴趣的:(最短路,迪杰斯特拉算法)