LightOJ 1074 Extended Traffic

题目链接

Description

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组测试样例,每一组第一行一个n,表示有n个点,之后是n个点对应的数值,之后一个m表示有m个联系,每一行两个数a、b,表示a可以走到b,其中所需要的花费是(b的数值-a的数值)的三次方,问从第一个点开始到所有点的距离,如果到达的点位于某个环或者数值小于3或者抵达不到就输出?否则输出对应的距离

具体思路
三次方的运算可能存在负环,于是用spfa算法,在最里层判断负环的时候利用一个dfs的搜索,搜索位于负环上的所有点,这些点输出均是?之后对整体的距离数组dis进行判断,如果是负环上的点或是小于3或是无穷大就是无法到达,对于此题如果直接进行spfa+dfs会爆炸,需要对原有的spfa进行剪枝头,如果某个点已经是位于负环上,就直接跳过,不需要进行spfa

#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<stack>
#include<math.h>
using namespace std;
typedef long long int LLD;
const LLD maxnum=205;
LLD pay[maxnum],ans[maxnum],dis[maxnum];
struct node
{
    LLD from,to,cost;
};
vector<node>line[maxnum];
void addline(LLD from,LLD to)
{
    node now;
    now.from=from;
    now.to=to;
    now.cost=(pay[to]-pay[from])*(pay[to]-pay[from])*(pay[to]-pay[from]);
    line[now.from].push_back(now);
    return ;
}
void dfs(LLD pot)
{
    ans[pot]=1;
    for (LLD i=0;i<line[pot].size();i++)
    {
        if (!ans[line[pot][i].to])
        {
            dfs(line[pot][i].to);
        }
    }
    return ;
}
void spfa(LLD n)
{
    LLD flag[maxnum],flagnum[maxnum];
    fill(dis,dis+maxnum,0x7fffffff);
    memset(flag,0,sizeof(flag));
    memset(flagnum,0,sizeof(flagnum));
    memset(ans,0,sizeof(ans));
    queue<LLD>dui;
    dis[1]=0;
    flag[1]=1;
    flagnum[1]++;
    dui.push(1);
    while (!dui.empty())
    {
        LLD now=dui.front();
        dui.pop();
        flag[now]=0;
        for (LLD i=0;i<line[now].size();i++)
        {
            node next=line[now][i];
            if (ans[next.to])
            {
                continue;
            }
            if (dis[next.to]>dis[next.from]+next.cost)
            {
                dis[next.to]=dis[next.from]+next.cost;
                if (!flag[next.to])
                {
                    flag[next.to]=1;
                    dui.push(next.to);
                    flagnum[next.to]++;
                    if (flagnum[next.to]>n)
                    {
                        dfs(next.to);
                    }
                }
            }
        }
    }
}
int main()
{
    LLD t;
    scanf("%lld\n",&t);
    for (LLD T=1;T<=t;T++)
    {
        LLD n;
        scanf("%lld\n",&n);
        for (LLD i=1;i<=n;i++)
        {
            scanf("%lld",&pay[i]);
            line[i].clear();
        }
        LLD m;
        scanf("%lld",&m);
        for (LLD i=1;i<=m;i++)
        {
            LLD from,to;
            scanf("%lld %lld",&from,&to);
            addline(from,to);
        }
        spfa(n);
        printf("Case %lld:\n",T);
        LLD q;
        scanf("%lld",&q);
        while (q--)
        {
            LLD to;
            scanf("%lld",&to);
            if (ans[to]||dis[to]<3||dis[to]==0x7fffffff)
            {
                printf("?\n");
            }else
            {
                printf("%lld\n",dis[to]);
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值