最短路SPFA—Extended Traffic(LightOJ - 1074)

题目链接:>_>

题目:Extended Traffic

Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked in congestion. In order to convince people avoid shortest routes, and hence the crowded roads, to reach destination, the city authority has made a new plan. Each junction of the city is marked with a positive integer (≤ 20) denoting the busyness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets the amount (busyness of destination - busyness of source)3 (that means the cube of the difference) from the traveler. The authority has appointed you to find out the minimum total amount that can be earned when someone intelligent goes from a certain junction (the zero point) to several others.

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains n integers denoting the busyness of the junctions from 1 to n respectively. The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.

Output

For each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a ‘?’.

Sample Input

2
5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5

2
10 10
1
1 2
1
2

Sample Output

Case 1:
3
4
Case 2:
?

题意:

有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(Av−Au)3。问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时间小于3输出?否则输出所花的时间。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int maxm = 100005;
const int inf = 0x73f3f3f;
int n, m;
struct node {
    int to, next, w;
}x[maxm];
int cnt;
int head[maxm];
int dis[maxn], vis[maxn], outqueue[maxm];
int v2[10005];
void add(int start, int endd, int w) {
    x[cnt].w = w;
    x[cnt].to = endd;
    x[cnt].next = head[start];
    head[start] = cnt++;
}
void init() {
    cnt = 0;
    memset(head, -1, sizeof head);
    memset(x, 0, sizeof x);
    memset(dis, inf, sizeof dis);
    memset(vis, 0, sizeof vis);
    memset(v2, 0, sizeof v2);
    memset(outqueue, 0, sizeof outqueue);
}
void dfs(int u) {
    v2[u] = 1;
    for (int i = head[u]; i != -1; i = x[i].next) {
        if (!v2[x[i].to])dfs(x[i].to);
    }
}
bool spfa() {
    queue<int>q;
    q.push(1);
    dis[1] = 0;
    vis[1] = 1;
    outqueue[1]++;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = head[u]; i != -1; i = x[i].next) {
            int v = x[i].to;
            if (v2[v])continue;
            if (dis[v] > dis[u] + x[i].w) {
                dis[v] = dis[u] + x[i].w;
                if (!vis[v]) {
                    vis[v] = 1;
                    q.push(v);
                    outqueue[u]++;
                    if (outqueue[u] > n)dfs(u);
                }
            }
        }
    }
    return 1;
}
 
int mx[1005];
int main() {
    int t;
    scanf("%d", &t);
    for (int u = 1; u <= t; u++) {
        init();
        scanf("%d", &n);
        int i, j, k;
        for (i = 1; i <= n; i++) {
            scanf("%d", &mx[i]);
        }
        scanf("%d", &m);
        int q1, q2, q3;
        for (i = 0; i < m; i++) {
            scanf("%d %d", &q1, &q2);
            add(q1, q2, (mx[q2] - mx[q1]) * (mx[q2] - mx[q1]) * (mx[q2] - mx[q1]));
        }
        spfa();
        int mm = 0;
        scanf("%d", &mm);
        printf("Case %d:\n", u);
        //for (i = 0; i <= n; i++) {
        //  printf("%d ", dis[i]);
        //}cout << endl;
        for (i = 0; i < mm; i++) {
            scanf("%d", &q1);
            if (dis[q1] < 3||v2[q1]||dis[q1]>=inf)printf("?\n");
            else printf("%d\n", dis[q1]);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值