UVA 11280 Flying to Fredericton 最短路DP

Problem C

FLYING TO FREDERICTON

After being inspired by Salvador Dali's artwork, Brett decided he would like to travel to Fredericton, New Brunswick to visit the Beaverbrook Art Gallery.

Brett lives in Calgary, and would like to find the cheapest flight or flights that would take him to Fredericton. He knows that a direct flight from Calgary to Fredericton, if one exists, would be absurdly expensive, so he is willing to tolerate a certain number of stopovers. However, there are multiple airlines in Canada with so many different flights between different cities now, which makes it very difficult for Brett to find the least expensive way to Fredericton! Can you write a program to help Brett plan his route?

Map showing a sample of the flights that would take Brett to Fredericton.

You will be given a list of cities between and including Calgary and Fredericton. The cities will be given in order of "nearest" to "farthest". The first city will always be "Calgary" and the last "Fredericton".

You will also be given a list of flights between pairs of cities, and the associated cost for each flight, taxes included. There will never be a flight from a farther city to a nearer city - Brett has already discarded those flights, deeming them to be a waste of time and money. Bear in mind, however, that there may be more than one flight between any two cities, as Brett is considering flights from all airlines.

Finally, you are presented with a number of queries. Each query is a single integer indicating the maximum number of stopovers Brett will tolerate. For each query, your program must calculate the least total cost of flights that would take Brett from Calgary to Fredericton with no more than the requested number of stopovers.

Input

The first line of input contains a single number indicating the number of scenarios to process. A blank line precedes each scenario.

Each scenario begins with a number N (2 ≤ N ≤ 100), the number of cities, followed by N lines containing the names of the cities. A city name is a string of up to 20 uppercase and lowercase letters. Next is a number M (0 ≤ M ≤ 1000), the number of flights available, followed by M lines describing the flights. Each flight is described by its departure city, its destination city, and an integer representing its cost in dollars. The final line starts with Q (1 ≤ Q ≤ 10), the number of queries, followed by Q numbers indicating the maximum number of stopovers.

Output

For each scenario, your program should output the scenario number, followed by the least total cost of the flights for each query. Follow the format of the sample output. If no flights can satisfy a query, write "No satisfactory flights". Output a blank line between scenarios.

Sample Input

2

4
Calgary
Winnipeg
Ottawa
Fredericton
6
Calgary Winnipeg 125
Calgary Ottawa 300
Winnipeg Fredericton 325
Winnipeg Ottawa 100
Calgary Fredericton 875
Ottawa Fredericton 175
3 2 1 0

3
Calgary
Montreal
Fredericton
2
Calgary Montreal 300
Montreal Fredericton 325
1 0

Output for the Sample Input

Scenario #1
Total cost of flight(s) is $400
Total cost of flight(s) is $450
Total cost of flight(s) is $875

Scenario #2
No satisfactory flights
---------------

占坑

---------------

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=211;
const int maxm=2111;
map<string,int>mp;
char name[111111];
int S,T;


struct EdgeNode{
    int to;
    int w;
    int next;
};
struct BellmanFord{
    EdgeNode edges[maxm];
    int head[maxn],edge,n;
    bool inq[maxn][maxn];
    queue< pair<int,int> >que;
    int dis[maxn][maxn];
    void addedge(int u,int v,int c){
        edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
    }
    void init(int n){
        memset(head,-1,sizeof(head));
        edge=0;
        this->n=n;
    }
    bool spfa(int s,int len){
        int u;
        memset(dis,INF,sizeof(dis));
        memset(inq,0,sizeof(inq));
        while (!que.empty()) que.pop();
        dis[0][s]=0;
        inq[0][s]=true;
        que.push( make_pair(0,s) );
        while (!que.empty()){
            int tim=que.front().first;
            u=que.front().second;
            que.pop();
            inq[tim][u]=false;
            for (int i=head[u];i!=-1;i=edges[i].next){
                int v=edges[i].to;
                int w=edges[i].w;
                if ( tim+1<=len&&dis[tim+1][v]>dis[tim][u]+w ){
                    dis[tim+1][v]=dis[tim][u]+w;
                    //do something

                    //nice try
                    if (!inq[tim+1][v]){
                        inq[tim+1][v]=true;
                        que.push( make_pair(tim+1,v) );
                    }
                }
            }
        }
        return true;
    }
}solver;


int n,m;
int main()
{
    int TMD;
    int cas=0;
    scanf("%d",&TMD);
    while (TMD--){
        //input()
        scanf("%d",&n);
        solver.init(n);
        for (int i=1;i<=n;i++){
            scanf("%s",name);
            mp[name]=i;
        }
        scanf("%d",&m);
        for (int i=0;i<m;i++){
            int x,y,c;
            scanf("%s",name);
            x=mp[name];
            scanf("%s",name);
            y=mp[name];
            scanf("%d",&c);
            solver.addedge(x,y,c);
        }
        S=mp["Calgary"];
        T=mp["Fredericton"];
        //Solve()
        int Q,qst;
        printf("Scenario #%d\n",++cas);
        scanf("%d",&qst);
        while (qst--){
            scanf("%d",&Q);
            Q++;
            if (Q>n) Q=n;
            if (Q<1) Q=1;
            solver.spfa(S,Q);
            int ans=INF;
            for(int i=0;i<=Q;i++)
                ans=min(ans,solver.dis[i][T]);
            if (ans==INF){
                printf("No satisfactory flights\n");
            }else{
                printf("Total cost of flight(s) is $%d\n",ans);
            }
        }
        if (TMD!=0) puts("");
    }
    return 0;
}







内容概要:该论文深入研究了液压挖掘机动臂下降势能回收技术,旨在解决传统液压挖掘机能耗高的问题。提出了一种新型闭式回路势能回收系统,利用模糊PI自整定控制算法控制永磁无刷直流电动机,实现了变转速容积调速控制,消除了节流和溢流损失。通过建立数学模型和仿真模型,分析了不同负载下的系统性能,并开发了试验平台验证系统的高效性和节能效果。研究还涵盖了执行机构能量分布分析、系统元件参数匹配及电机控制性能优化,为液压挖掘机节能技术提供了理论和实践依据。此外,通过实验验证,该系统相比传统方案可降低28%的能耗,控制系统响应时间缩短40%,为工程机械的绿色化、智能化发展提供了关键技术支撑。 适合人群:从事工程机械设计、制造及维护的工程师和技术人员,以及对液压系统节能技术感兴趣的科研人员。 使用场景及目标:①理解液压挖掘机闭式回路动臂势能回收系统的原理和优势;②掌握模糊PI自整定控制算法的具体实现;③学习如何通过理论建模、仿真和实验验证来评估和优化液压系统的性能。 其他说明:此研究不仅提供了详细的理论分析和数学建模,还给出了具体的仿真代码和实验数据,便于读者在实际工作中进行参考和应用。研究结果表明,该系统不仅能显著提高能源利用效率,还能延长设备使用寿命,降低维护成本,具有重要的工程应用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值