forwhat00  
#include<iostream>
#include<string>
#include<map>
#include<vector>
#define INF 999999
using namespace std;
int min(int a,int b){return a<b?a:b;}
int N,totalplc=0;
vector<int> m[205];
map<string,int> place;
int findmap(int sta,int end){
    if(sta>end){return m[end][sta];}
    return m[sta][end];
}
void setmap(int sta,int end,int val){
    int t;
    if(sta>end){t=end;end=sta;sta=t;}
    if(sta==end){m[sta][end]=0;return;}
    m[sta][end]=val;
}
int main(){
    int a,b,c;
    string startplc,endplc,tsta,tend;
    
    while(cin>>N && N!=-1){
        cin>>startplc>>endplc;
        
        for(int yy=0;yy<=200;yy++){
            m[yy].resize(201);
            for(int xx=0;xx<=yy;xx++){
                setmap(xx,yy,INF);
            }
        }place.clear();totalplc=0;
        for(int i=0;i<N;i++){
            cin>>tsta>>tend>>c;
            a=place.count(tsta);
            b=place.count(tend);
            if(a==0){place[tsta]=totalplc++;}
            if(b==0){place[tend]=totalplc++;}
            setmap(place[tsta],place[tend],c);
        }
        if(startplc==endplc){cout<<0<<endl;continue;}
        for(int mid=0;mid<totalplc;mid++){
            for(int sta=0;sta<totalplc;sta++){
                for(int end=0;end<totalplc;end++){
                    a=findmap(sta,end);
                    b=findmap(sta,mid)+findmap(mid,end);
                    setmap(sta,end,min(a,b));
                }
            }
        }
        a=place.count(startplc);
        b=place.count(endplc);
        c=findmap(place[startplc],place[endplc]);

        if(a==0 || b==0 || c==INF)
            cout<<-1<<endl;
        else
            cout<<c<<endl;
    }
    return 0;

}

http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1002&cid=36602

posted on 2020-07-05 14:02  forwhat00  阅读(82)  评论(0编辑  收藏  举报