Light OJ 1074:Extended Traffic(spfa判负环)

Extended Traffic

题目链接:https://vjudge.net/problem/LightOJ-1074

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:

?

 

题意:

给出一个有向图,假设一条边为u->v,其边权为(v-u)^3,最后有多个询问,问从1出发,到达询问目的地的花费最小为多少。当花费小于3时,输出“?”。

 

题解:

建图很简单,直接三方建就是了。然后从一号点跑最短路,注意用spfa判下负环,负环能够到达的点也是“?”。

 

代码如下:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N  = 205;
int d[N],vis[N],c[N],a[N],check[N],fa[N],head[N];
int t,n,m,S;
int qp(int x){
    return x*x*x;
}
struct Edge{
    int u,v,w,next;
}e[N*N<<1];
int tot;
void adde(int u,int v,int w){
    e[tot].u=u;e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
int spfa(int s){
    queue <int> q;
    memset(d,INF,sizeof(d));memset(fa,-1,sizeof(fa));
    memset(vis,0,sizeof(vis));memset(c,0,sizeof(c));
    q.push(s);vis[s]=1;d[s]=0;c[s]=1;
    while(!q.empty()){
        int u=q.front();q.pop();vis[u]=0;
        if(c[u]>n){
            S=u;
            return -1;
        }
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(d[v]>d[u]+e[i].w){
                d[v]=d[u]+e[i].w;
                fa[v]=u;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                    c[v]++;
                }
            }
        }
    }
    return d[n];
}
void dfs(int s){
    check[s]=1;
    for(int i=head[s];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(!check[v]) dfs(v);
    }
}
int main(){
    int cnt = 0;
    cin>>t;
    while(t--){
        cnt++;
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        cin>>m;
        memset(check,0,sizeof(check));
        memset(head,-1,sizeof(head));tot=0;
        for(int i=1;i<=m;i++){
            int u,v;
            cin>>u>>v;
            adde(u,v,qp(a[v]-a[u]));
        }
        int flag = spfa(1);
        if(flag==-1){
            memset(check,0,sizeof(check));
            dfs(S);
        }
        int q;
        cin>>q;
        cout<<"Case "<<cnt<<":"<<endl;
        for(int i=1;i<=q;i++){
            int x;
            cin>>x;
            if(flag==-1 && check[x]) cout<<"?"<<endl;
            else if(d[x]<3 || d[x]==INF) cout<<"?"<<endl;
            else cout<<d[x]<<endl;
        }
    }
    return 0;
}

 

posted @ 2019-01-31 23:35  heyuhhh  阅读(128)  评论(0编辑  收藏  举报