lightoj 1074 - Extended Traffic(SPFA模板)

来源:互联网 发布:linux添加用户的命令 编辑:程序博客网 时间:2024/04/30 04:32

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

Output for 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

Case 1:

3

4

Case 2:

?



大意是给你一个有向图,每一个点都有一个权值,边的权值等于目的地的权值减出发地的权值的三次方,因为会有负边权,所以用SPFA,注意要判断一下负环。

#include<queue>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define endl '\n'using namespace std;const int inf = 1e9;int n,m;int a[220],dis[220],b[220];int vis[220],cnt[220];struct edge{    int to,cost;};vector<edge> e[220];void dfs(int x){    vis[x] = 1;    for(int i=0;i<e[x].size();i++)        if(!vis[e[x][i].to])            dfs(e[x][i].to);}void SPFA(int s){    memset(b,0,sizeof(b));    memset(cnt,0,sizeof(cnt));    memset(vis,0,sizeof(vis));    queue<int> q;    for(int i=1;i<=n;i++)        dis[i] = inf;    dis[s] = 0;    q.push(s);    b[s] = 1;    while(!q.empty())    {        int t = q.front();        q.pop();        b[t] = 0;        for(int i=0;i<e[t].size();i++)        {            int x = e[t][i].to;            if(vis[x])                continue;            if(dis[x] > dis[t] + e[t][i].cost)            {                dis[x] = dis[t] + e[t][i].cost;                if (b[x] == 0)                {                    q.push(x);                    b[x] = 1;                    cnt[x]++;                    if(cnt[x] > n)                        dfs(x);                }            }        }    }}int main(void){    int T,i,j,q;    scanf("%d",&T);    int cas = 1;    while(T--)    {        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);            e[i].clear();        }        scanf("%d",&m);        for(i=1;i<=m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            edge t;            t.to = y;            t.cost = (a[y]-a[x])*(a[y]-a[x])*(a[y]-a[x]);            e[x].push_back(t);        }        SPFA(1);        scanf("%d",&q);        printf("Case %d:\n",cas++);        while(q--)        {            int x;            scanf("%d",&x);            if(dis[x] == inf || dis[x] < 3)                printf("?\n");            else                printf("%d\n",dis[x]);        }    }    return 0;}


0 0
原创粉丝点击