其他分享
首页 > 其他分享> > O - Extended Traffic

O - Extended Traffic

作者:互联网

O - 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:

?

题意:给出一个T,表示T组样例,然后给出一个n,然后n个数,表示n个地方的繁忙程度,然后给出一个m,下面给出m对数(u,v)表示可以从u到v ,这条路的权值为(v的繁忙程度 - u的繁忙程度)^3, 然后给出一个q我的代码中为p,然后给出p个要查询的点,然后查询从1号点到所查询点的最短路。

题解:因为是减法,所以会存在负环,所以用到spfa算法,如果存在负环,说明一定小于3,直接return;当然这是模板,详细请看代码:

代码:

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int MAX = 200+10;
const int inf = 0x3f3f3f3f;//判断负环用inf,正环为0
int vis[MAX],dis[MAX],sum[MAX],busy[MAX],head[MAX];
int n,m,tot,p;
struct hh
{
    int to,nt,w;
} edge[MAX*MAX]; //任意两个点可能都有边,所以用MAX*MAX保证数组够用
void init() //初始化
{
    memset(vis,0,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    memset(sum,0,sizeof(sum));
    memset(head,-1,sizeof(head));
    tot=0;
}
void add(int u,int v,int w) //建图模板
{
    edge[tot].to=v;
    edge[tot].w=w;
    edge[tot].nt=head[u];
    head[u]=tot++;
}
void spfa(int s) //判断负环模板
{
    queue<int> q;
    q.push(s);
    dis[s]=0;
    sum[s]++;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        vis[x]=0;//前往别忘了写,这里我一直有点迷
        for (int i = head[x]; ~i; i = edge[i].nt)
        {
            int y=edge[i].to;
            if(dis[y]>dis[x]+edge[i].w)
            {
                dis[y]=dis[x]+edge[i].w;
                if(!vis[y])
                {
                    q.push(y);
                    vis[y]=1;
                    if(++sum[y]>=n)
                        return;//判断是否存在负环,如果一个点出现的次数>=总的点数说明存在负环
                }
            }
        }
    }
}
int main()
{
    int T;
    cin >> T;
    int cas=1;
    while(T--)
    {
        init();
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> busy[i];
        }
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            int u,v;
            cin >> u >> v;
            int w=(busy[v]-busy[u])*(busy[v]-busy[u])*(busy[v]-busy[u]);//计算权值
            add(u,v,w);
        }
        spfa(1);
        cin >> p;
        cout << "Case " << cas++ << ":" << endl;
        while(p--)
        {
            int x;
            cin >> x;
            if(dis[x]<3||dis[x]==inf)
                cout << "?" << endl;//小于三或者不连通输出?,题目里规定的
            else
                cout << dis[x] << endl;
        }
    }
    return 0;
}

参考:https://blog.csdn.net/lgz0921/article/details/82013974

标签:Extended,junction,int,MAX,edge,busy,Traffic,dis
来源: https://blog.csdn.net/CHEN_XU_LING/article/details/110306101