SPFA判负环-LightOJ-1074-Extended Traffic

来源:互联网 发布:制冷蒸发器设计软件 编辑:程序博客网 时间:2024/05/17 06:44

Extended Traffic
Time Limit:2000MS Memory Limit:32768KB

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个点有着自己的busy值,再给你m条单向边,边的权值为目标点的busy值减去源点的busy值的差的立方。每个点至多有一条通往其他点的边。现在再向你查询q个点的最短距离,如果距离小于3或者不可达此点,就输出问号。


由题可知,会有负权边的存在,所以不能用dijkstra,只能用SPFA,将负环上的点都标记一下,查询时直接输出问号,其他的正常输出。


////  main.cpp//  LightOJ-1074-Extended Traffic////  Created by 袁子涵 on 15/12/6.//  Copyright © 2015年 袁子涵. All rights reserved.////  52ms    2096KB#include <iostream>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <math.h>#include <algorithm>#include <vector>#include <queue>#define INF 0x3f3f3f3f#define MAXN 205using namespace std;int T,n,m;long long int busy[MAXN];struct qnode{    int v;    long long int c;    qnode(int _v=0,long long int _c=0):v(_v),c(_c){}    bool operator <(const qnode &r)const    {        return c>r.c;    }};struct Edge{    int v;    long long int cost;    Edge(int _v=0,long long int _cost=0):v(_v),cost(_cost){}};vector<Edge>E[MAXN];bool vis[MAXN];long long int dist[MAXN];int cnt[MAXN];bool re[MAXN];void dfs(int u){    re[u]=1;    for(int i = 0;i < E[u].size();i++)        if(!re[E[u][i].v])            dfs(E[u][i].v);}void SPFA(){    memset(vis, 0, sizeof(vis));    memset(re, 0, sizeof(re));    for (int i=1; i<=n; i++) {        dist[i]=INF;    }    vis[1]=1;    dist[1]=0;    queue<int>que;    while (!que.empty()) {        que.pop();    }    que.push(1);    memset(cnt, 0, sizeof(cnt));    cnt[1]=1;    while (!que.empty()) {        int u=que.front();        que.pop();        vis[u]=0;        for (int i=0; i<E[u].size(); i++) {            int v=E[u][i].v;            if (re[v]) {                continue;            }            if (dist[v]>dist[u]+E[u][i].cost) {                dist[v]=dist[u]+E[u][i].cost;                if (!vis[v]) {                    vis[v]=1;                    que.push(v);                    if (++cnt[v]>n) {                        dfs(v);                    }                }            }        }    }}void addedge(int u,int v,long long int w){    E[u].push_back(Edge(v,w));}int main(int argc, const char * argv[]) {    int num1,num2;    cin >> T;    int t=0;    while (t!=T) {        t++;        scanf("%d",&n);        for (int i=0; i<=n; i++) {            E[i].clear();        }        for (int i=1; i<=n; i++) {            scanf("%lld",&busy[i]);        }        scanf("%d",&m);        for (int i=1; i<=m; i++) {            scanf("%d %d",&num1,&num2);            long long int tmp=(busy[num2]-busy[num1])*(busy[num2]-busy[num1])*(busy[num2]-busy[num1]);            addedge(num1,num2,tmp);        }        SPFA();        scanf("%d",&num1);        printf("Case %d:\n",t);        for (int i=1; i<=num1; i++) {            scanf("%d",&num2);            if (dist[num2]==INF || dist[num2]<3 || re[num2]) {                printf("?\n");            }            else                printf("%lld\n",dist[num2]);        }    }    return 0;}
0 0
原创粉丝点击