HDU2112(hash+dijkstra)

#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<cstring>
#define inf (0x3f3f3f3f)
using namespace std;
const int maxn = 150 + 15;
int Grape[maxn][maxn];
map<string,int> m;
int d[maxn];// s 到每个节点的距离
bool vis[maxn];
string startPos,endPos;
int cnt;
void  dijkstra()
{
    memset(vis,false,sizeof(vis));
    memset(d,inf,sizeof(d));
    int start = m[startPos];
    int end = m[endPos];//start to end 单源最短距离
    d[start] = 0;
    while(true)
    {
        int mincost = inf,u = 0;
        for(int v=1;v<=cnt;++v)
        {
            if(!vis[v]&&d[v]<mincost)
            {
                mincost = d[v];
                u = v;
            }
        }
        if(mincost==inf)
            break;
        vis[u] = true;
        for(int v=1;v<=cnt;++v)
        {
            if(!vis[v]&&d[u]+Grape[u][v]<d[v])
            {
                d[v] = Grape[u][v] + d[u];
            }
        }
    }
    if(d[end]==inf)
        cout<<"-1"<<endl;
     the else 
        COUT << D [End] << endl; 
} 
int main () // the hash built Grape seeking single source shortest path 
{
     int n-;
     the while (CIN >> n-n-&& = -! . 1 ) 
    { 
        Memset (Grape, INF , the sizeof (Grape)); // INF node does not communicate on behalf of uv 
        CNT = 0 ; // Construction s - e bidirectional FIG 
        m.clear (); 
        CIN >> >> the startPos endpos; 
        m [the startPos] = ++ CNT; 
        m [endpos] = ++ CNT;
         String POS1, POS2;// POS1 to build and POS2 from the side 
        IF (n-== 0 ) 
        { 
            IF (the startPos == endpos) 
                COUT << 0 << endl;
             the else 
                COUT << - . 1 << endl;
             Continue ; 
        } 
        the while (N-- ) 
        { 
            CIN >> >> POS1 POS2;
             int U, V;
             if (! m.count (POS1)) 
                m [POS1] = CNT ++; // from the start node to build a node 
            if(! M.count (POS2)) 
                m [POS2] = ++ CNT; 
            U = m [POS1]; 
            V = m [POS2];
             int W; // edge weight value 
            CIN >> W; 
            Grape [U] [V] = Grape [V] [U] = W; 
        } // build FIG 
        Dijkstra (); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/newstartCY/p/11563687.html