LightOJ 1074 O

来源:互联网 发布:菜鸟网络天津武清园区 编辑:程序博客网 时间:2024/06/03 16:44

Problem 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:
?

大致题意:有编号1~n的n个城市,每个城市有一个拥挤度,val[i]表示城市i的拥挤度,从城市i到j所花的时间是(val[j]-val[i])^3,求第一个城市到第k个城市的最短路,如果无法到达或者结果小于3输出‘?’。

思路:可能存在负环,所以我们可以用spfa判断负环,然后把负环上所有的点都标记一下,因为它们一定小于3。

代码如下

/*采用链式前向星存储边*/#include<iostream>#include<algorithm>#include<string>#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<cstring>using namespace std;const int Maxn=100010;const int NN=210;const int INF =0x3f3f3f3f;struct Edge{    int to,cost;     int next;}edge[2*Maxn];int tol;int head[NN];//head[i]存储当前以i点为起点的边的编号 int dis[NN];int val[NN];int cnt[NN];//记录点入队次数bool r[NN];//标记负环点bool vis[NN];int n,m; void dfs(int x)//标记负环上的点 {    r[x]=1;    for(int i=head[x];i!=-1;i=edge[i].next)        if(!r[edge[i].to])          dfs(edge[i].to); } void SPFA() {    memset(vis,0,sizeof(vis));    memset(r,0,sizeof(r));    memset(cnt,0,sizeof(cnt));    for(int i=1;i<=n;i++)    dis[i]=INF;    dis[1]=0;    queue<int>que;    que.push(1);    while(!que.empty())    {        int x=que.front();        vis[x]=0;        que.pop();        for(int i=head[x];i!=-1;i=edge[i].next)        {            int to=edge[i].to;            if(r[to])                continue;            if(dis[to]>dis[x]+edge[i].cost)            {                dis[to]=dis[x]+edge[i].cost;                if(!vis[to])                {                    vis[to]=1;                    que.push(to);                    if(++cnt[to]>=n)                     dfs(to);                 }             }         }     } }void addedge(int u,int v,int cost){    edge[tol].to=v;    edge[tol].cost=cost;    edge[tol].next=head[u];    head[u]=tol++; } int Cost(int x){    return x*x*x;}int main(){    int T;    scanf("%d",&T);    for(int t=1;t<=T;t++)    {        tol=0;        scanf("%d",&n);        for(int i=1;i<=n;i++)        head[i]=-1;        for(int i=1;i<=n;i++)        scanf("%d",&val[i]);        scanf("%d",&m);        while(m--)        {            int x,y;            scanf("%d%d",&x,&y);            addedge(x,y,Cost(val[y]-val[x]));         }        SPFA();        int q;        scanf("%d",&q);        printf("Case %d:\n",t);        while(q--)        {            int k;            scanf("%d",&k);            if(r[k]||dis[k]<3||dis[k]==INF)            printf("?\n");            else             printf("%d\n",dis[k]);        }    }   return 0;}
0 0