UVA - 11280 Flying to Fredericton(SPFA)

题目大意:给出N个点,M条线,Q个询问,问从起点到终点,最多经过k个点的最短线路

解题思路:spfa直接跑,然后纪录最短路的数组加一个维度,纪录经过几个点就可以了,还是挺水的

#include <cstdio>
#include <cstring>
#include <map>
#include <iostream>
#include <queue>
using namespace std;

#define N 110
#define M 1010
#define INF 0x3f3f3f3f

struct Edge{
    int u, v, next, c;
}E[M];

struct Node {
    int u, time;
    Node() {}
    Node(int u, int time): u(u), time(time) {}
}n1, n2;

map<string, int> city;
int n, m, tot, q;
int head[N], d[N][N];
bool vis[N][N];

void AddEdge(int u, int v, int c) {
    E[tot].v = v;
    E[tot].c = c;
    E[tot].next = head[u];
    head[u] = tot++;
}

void init() {
    city.clear();
    scanf("%d", &n);
    string a, b;
    for (int i = 1; i <= n; i++) {
        cin >> a;
        city[a] = i;
    }

    memset(head, -1, sizeof(head));
    tot = 0;

    scanf("%d", &m);
    int c;
    for (int i = 0; i < m; i++) {
        cin >> a >> b >> c;
        AddEdge(city[a], city[b], c);
    }
}

int s, t;
void spfa() {
    memset(d, 0x3f, sizeof(d));
    memset(vis, 0, sizeof(vis));
    queue<Node> Q;
    Q.push(Node(s,0));
    d[s][0] = 0;

    while (!Q.empty()) {
        n1 = Q.front();
        Q.pop();

        int u = n1.u, time = n1.time;
        for (int i = head[n1.u]; ~i; i = E[i].next) {
            int v = E[i].v;
            if (d[u][time] + E[i].c < d[v][time + 1]) {
                d[v][time + 1] = d[u][time] + E[i].c;
                if (!vis[v][time + 1]) {
                    vis[v][time + 1] = true;
                    Q.push(Node(v, time + 1));
                }
            }
        }
    }
}

void solve() {
    s = city["Calgary"]; t = city["Fredericton"];
    spfa();

    scanf("%d", &q);
    int Max;
    while (q--) {
        scanf("%d", &Max);
        Max = min(Max, n);
        int ans = INF;
        for (int i = 0; i <= Max + 1; i++)
            ans = min(ans, d[t][i]);
        if (ans == INF)
            printf("No satisfactory flights\n");
        else
            printf("Total cost of flight(s) is $%d\n", ans);
    }

}

int main() {
    int test, cas = 1;
    bool flag = false;
    scanf("%d", &test);
    while (test--) {
        if (flag)
            printf("\n");
        flag = true;
        printf("Scenario #%d\n", cas++);
        init();
        solve();
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 10:59:23

UVA - 11280 Flying to Fredericton(SPFA)的相关文章

UVA 11280 - Flying to Fredericton(最短路)

UVA 11280 - Flying to Fredericton 题目链接 题意:给定一些国家,和两个国家间的花费,现在有一些询问,询问每次最多转k次飞机,最小花费 思路:dijkstra变形,多开一维表示转机次数即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <iostream> #include <strin

uva 11280 状态压缩+最短路

题意:坐飞机从 a 地到 b 地 ,在最多停留s次时 , 最小花费是多少? 在题目给出的地点 , 是按从远到近给出的 , 并且给出的航班中 , 不会有从远地点到近地点的航班. 因此从这可以看出 , 题目给的图是一个DAG图 , 那么我们就能用toposort来找最短路. 注意: 会有重边 解法: 构造一个数组 d[i][j]  , 表示从开始点 s  到点 i , 在停留 j 次时的最小花费. 然后我们再求出这个图的toposort , 再求这个每一个点和其相邻点的距离. 代码: #includ

[题解]UVa 12661 Funny Car Racing - spfa

很简单的一道最短路问题.分情况处理赛道的打开和关闭. Code 1 /** 2 * UVa 3 * Problem#12661 4 * Accepted 5 * Time:50ms 6 */ 7 #include<iostream> 8 #include<fstream> 9 #include<sstream> 10 #include<cstdio> 11 #include<cstdlib> 12 #include<cstring>

uva 11374 Airport Express(spfa 邻接表+队列)

Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and theCommercial-Xpress

UVA-11280 Flying to Fredericton (dijkstra)

题目大意:一张有向图,n个节点,m条边,有边权.求从起点到终点在最多经过s个中间节点(不包括始末点)时的最小权和. 题目分析:因为起点和终点是固定的,只需一次dijkstra打出表dis[u][k],查表即可.dis[u][k]表示经过k个中间节点到达u点时的最小费用.要注意,经过的中间节点数不会超过n. 代码如下: # include<iostream> # include<cstdio> # include<map> # include<vector>

【Uva 11280 飞到弗雷德里顿】

·你可以尽情地坐飞机,但停留次数遭到限制. ·英文题,述大意:       给出一张有向图,起点是输入的第一个城市,终点是输入的最后一个城市.给出q个询问,每个询问含一个t,表示中途最多经过个城市的情况下,起点到终点的最短路径长度(即费用). ·分析:      经过的点的次数被限制,我们不禁想到使用二元组:(u,t)来表示从起点到节点u最多途径t个点(包括起点)时的最短路径长度. ·BellMan-Ford算法的精髓:枚举每条边,对该边的两点进行路径更新.不要学习了SPFA就忘记了它是哪里来的

UVA 10986 Sending email(SPFA)

There are n SMTP servers connected by network cables. Each of the m cables connects two computers and has a certain latency measured in milliseconds required to send an email message. What is the shortest time required to send a message from server S

UVA 558 Wormholes 【SPFA 判负环】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=499 题意:就是判断图中有无负环 SPFA,某个节点入队次数大于n就是有负环. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <algori

The Postal Worker Rings Once(UVA 117)最短路径—SPFA算法+邻接表

The Postal Worker Rings Once From:UVA, 117 Time Limit: 3000 MS Background Graph algorithms form a very important part of computer science and have a lineage that goes back at least to Euler and the famous Seven Bridges of K?nigsberg problem. Many opt