Shortest (Dijstra)

Problem Description
In the annual school competition, all students finalists will receive a very nice t-shirt. But whenever our staff to hundreds of pieces of clothing shipped back to the stadium from the store when they are very tired! So now they want to find the shortest route to the stadium from the store, you can help them?

 

Input
Comprising a plurality of sets of input data. The first line of each two integers N, M (N <= 100 , M <= 10000), N represents a few street intersections Chengdu, reference numeral 1 is to store the location of the intersection, the intersection designated N is the location of the stadium, M indicates a couple of choices in Chengdu. N = M = 0 indicates the end of input. Next M rows, each row comprising three integers A, B, C (1 < = A, B <= N, 1 <= C <= 1000), indicates there is a path between the intersection A and the intersection B, we staff need to C-minute time traveled this road.
Input line to ensure the presence of at least one track to store.
 

Output
For each input and output line, it represents the track workers come from the store minimum time
 

Sample Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
 

Sample Output
3 2

 

On a wave of http://acm.hdu.edu.cn/showproblem.php?pid=2544 code

 

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
typedef long long ll;
using namespace std;
const int INT=1e6+5;
#define lson rt<<1, l, m  
#define rson rt<<1|1, m+1, r
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define cn cin>>
#define ct cout<<
#define en <<endl
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define mem(s,t) memset(s,t,sizeof(s))
#define re return 0;
string str1="CC",str2="PC",str3="PP",str4="CP";
int dp[101][101],dis[101],used[101];
int m,n,x,y,z;
void Dijstra()
{
    mem(used,0);
    rep(1,n) dis[i] = dp[1][i];     //距离初始化
    dis[1] = 0;
    rep(1,n)
    {
        int index , mindis = INT;
        for(int j=1;j<=n;j++)
        {
            if( !used[j] && dis[j]<mindis )
            {
                index = j;
                mindis = dis[j];
            }
        }
        used[index] = 1;
        for(int j=1;j<=n;j++)
        {
            if( dis[index] + dp[index][j] < dis[j] )
                dis[j] = dis[index] + dp[index][j];
        }
    }
    ct dis[n] en;
}
void init()
{
    while(cin>>n>>m&&(n&&m))  // n-m points edges 
    {
         IF (n-== . 1 ) {CT 0 EN; Continue ;} 
        
        // weight Initial maximized 
        for ( int I = . 1 ; I <= n-; I ++ )
             for ( int J = . 1 ; J <= n-; J ++ ) 
                DP [I] [J] = the INT;
         // weight initialization 
        REP ( . 1 , m) 
        { 
            CIN >> >> X >> Y Z;
             IF (DP [X] [Y ]> Z) 
                DP [X] [Y]= dp[y][x] = z; 
        }
        Dijstra();
        /*
        rep(1,n)
            for(int j=1;j<=n;j++)
                dp[i][j] = dp[j][i] = i^j; 
        */
    }
}
int main()
{
    init();
}
View Code

 

----------------------------------------------- separator - ---------------------------------------

 Shortest do need to pay attention to the data range and optimize the point of (additional)  

1.floyd algorithm commonly used words adjacency matrix calculation (matrix initialization data not too much)

 

2.dijkstra algorithm then optimized heap together, that is the priority queue (if the heap plus map, increase the delete operation will be faster)

  Adapted to have n points, edges in FIG. M, given the starting point s, the shortest path can be determined arrival point of each other (if any)

 

3.SPFA algorithm, on the storm force wide search relaxation, a good mind to write pretty fast, in essence, BE optimization. Not available divergent view trellis diagram or the like 

 

----------------------------------------------- separator - ---------------------------------------

 

Dijstra general idea is to use the algorithm:

1) maximizing the path (to maximize main role is to screen out some of the non-existent road)

2) the presence of the path input, initializes all unvisited point source and the distance to each point stored DIS [] array of the direct distance (dis storage source to the respective points)

Dijstra core code

3) one by one point judgment, find the shortest distance from each point source, and updates the state of the point

4)! ! ! (Knock on the blackboard)

           if( dis[index] + dp[index][j] < dis[j] )
                  dis[j] = dis[index] + dp[index][j];

Update shortest distance

 

The figure below in conjunction with the above steps will be better understood (boo !!! figure is someone else's, not quiet)

 

 

 

 

 

 

 

1) to the weight of the edge 
2) optimization stack

 

 

 

 

 

 

 

Problem Description
In the annual school competition, all students finalists will receive a very nice t-shirt. But whenever our staff to hundreds of pieces of clothing shipped back to the stadium from the store when they are very tired! So now they want to find the shortest route to the stadium from the store, you can help them?

 

 

Input
Comprising a plurality of sets of input data. The first line of each two integers N, M (N <= 100 , M <= 10000), N represents a few street intersections Chengdu, reference numeral 1 is to store the location of the intersection, the intersection designated N is the location of the stadium, M indicates a couple of choices in Chengdu. N = M = 0 indicates the end of input. Next M rows, each row comprising three integers A, B, C (1 < = A, B <= N, 1 <= C <= 1000), indicates there is a path between the intersection A and the intersection B, we staff need to C-minute time traveled this road.
Input line to ensure the presence of at least one track to store.
 

 

Output
For each input and output line, it represents the track workers come from the store minimum time
 

 

Sample Input
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
 

 

Sample Output
3 2

 

Guess you like

Origin www.cnblogs.com/Shallow-dream/p/11410069.html