uva 11280

题目链接:https://vjudge.net/problem/UVA-11280

AC代码

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

vector<pair<int,int>>maps[103];
int s,e;
int q;
long long dis[130][1000];
struct node{
    int pos;
    int money;
    int tot;
    friend bool operator<(const node &a,const node &b)
    {
        return a.money>b.money;
    }
};

long long spfa()
{
    memset(dis,127,sizeof(dis));
    node n1,n2;
    dis[s][0]=0;
    n1.pos=s;
    n1.money=0;
    n1.tot=0;
    priority_queue<node>pq;
    pq.push(n1);
    while(!pq.empty())
    {
        n1=pq.top();
        pq.pop();
        if(n1.tot>q+1)
            continue;
        if(n1.pos==e)
            return n1.money;
        for(int i=0;i<maps[n1.pos].size();++i)
        {
            n2.pos=maps[n1.pos][i].first;
            n2.money=maps[n1.pos][i].second+n1.money;
            n2.tot=n1.tot+1;
            if(dis[n2.pos][n2.tot]>n2.money)
            {
                dis[n2.pos][n2.tot]=n2.money;
                pq.push(n2);
            }

        }
    }
    return -1;
}

int main()
{
    int t;
    int ca=1;
    cin>>t;
    while(t--)
    {

        map<string,int>she;
        int n;
        cin>>n;
        for(int i=1;i<=n;++i)
        {
            maps[i].clear();
            string str;
            cin>>str;
            she[str]=i;
            if(str=="Calgary")
                s=i;
            if(str=="Fredericton")
                e=i;
        }
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        {
            string str1,str2;
            int tt;
            cin>>str1>>str2>>tt;
            maps[she[str1]].push_back(make_pair(she[str2],tt));
        }
        printf("Scenario #%d\n",ca++);
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&q);
            long long ans=spfa();
            if(ans==-1)
                printf("No satisfactory flights\n");
            else
                cout<<"Total cost of flight(s) is $"<<ans<<endl;
        }
        if(t)
            cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36921652/article/details/82871758
今日推荐