FIG special - Shortest Path Variant

https://vjudge.net/problem/POJ-2263
which will become the shortest overall path of seeking maximum traffic in a road.
Only need to change the conditions relaxed to search for the most of a short circuit

/**
*	POJ-2263
*	spfa
*	@author Hongchuan CAO
**/
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<queue>

using namespace std;

const int Maxx = -1;

class Node{
    public:
        string end;
        int val;

        Node(string end,int val){
            this->end = end;
            this->val = val;
        }
};


int index = 0;
int n,r;
string ss,ee;

int num = 1;
map<string,int> ma;
vector<Node> path[220];
int dis[220];
bool use[220];

void clear(){
    ma.erase(ma.begin(),ma.end());//map clear
    for(int i=0;i<220;i++){
        path[i].clear();
        dis[i] = Maxx;
        use[i] = false;
    }
}

void add_side(string start,string end,int val){
    if(ma[start]==0) ma[start] = num++;
    if(ma[end]==0) ma[end] = num++;
    path[ma[start]].push_back(Node(end,val)); 
}

void get_data(){
    while(r--){
        string start,end;
        int val;
        cin>>start>>end>>val;
        add_side(start,end,val);
        add_side(end,start,val);
    }
    cin>>ss>>ee;
}

void spfa(){
    queue<int> que;
    que.push(ma[ss]);
    use[ma[ss]] = true;
    dis[ma[ss]] = 1e9;


    while(!que.empty()){
        int cur = que.front();
        que.pop();
        use[cur] = false;
        //relax
        for(int i=0;i<path[cur].size();i++){
            Node xx = path[cur][i];
            int x_to = ma[xx.end];
            //relax condition
            if(dis[x_to]< min(dis[cur],xx.val)){
                dis[x_to] = min(dis[cur],xx.val);
                if(!use[x_to]){
                    que.push(x_to);
                    use[x_to] = true;
                }
            }
        }        

    }

}

void output(){
    printf("Scenario #%d\n",index);
    printf("%d tons\n",dis[ma[ee]]);
    printf("\n");
}

void solve(){
    while(scanf("%d%d",&n,&r)!=EOF&&n!=0&&r!=0){
        index++;
        clear();
        get_data();
        spfa();
        output();
    }
}

int main(){
    solve();
    system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/baidu_41560343/article/details/91890166