lightOJ1074 spfa

题目地址在这里
题目描述
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

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

题目大意是给n个点,每个点有自己的权值val。给出m条边,每条边给出u和v,边的权值就是(val[v] - val[u])的三次方(所以有可能是负的)。求某点的最短路。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <math.h>
using namespace std;
const int MaxN = 210;
const int INF =  0x3f3f3f3f;
struct Edge{
    int v, val, nxt;
}edge[MaxN * MaxN / 2];
int par[MaxN], tol;
int dis[MaxN];
int val[MaxN];
bool vis[MaxN];
bool r[MaxN];
int n, m;
int t;

void init(){
    memset(dis, 0x3f, sizeof(dis));
    memset(par, -1, sizeof(par));
    memset(vis, false, sizeof(vis));
    memset(r, false, sizeof(r));
    tol = 0;
}

void dfs(int u){
    r[u] = true;
    for(int i = par[u]; i != -1; i = edge[i].nxt){
        if(!r[edge[i].v])   dfs(edge[i].v);
    }
}

void addEdge(int u, int v, int val){
    edge[tol].v = v;
    edge[tol].val = val;
    edge[tol].nxt = par[u];
    par[u] = tol++;
}

void spfa(int u){
    if(r[u])    return;
    vis[u] = true;
    for(int i = par[u]; i != -1; i = edge[i].nxt){
        int v = edge[i].v;
        int val = edge[i].val;
        if(dis[v] > dis[u] + val){
            dis[v] = dis[u] + val;
            if(vis[v]){
                dfs(v);
                return;
            }
            else
                spfa(v);
        }
    }
    vis[u] = false;
}

int getDis(int u, int v){
    int x = val[v] - val[u];
    return x * x * x;
}

int main(){
    scanf("%d", &t);
    for(int C = 1; C <= t; ++C){
        init();
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i){
            scanf("%d", val + i);
            //printf("val:%d\n", val[i]);
        }
        scanf("%d", &m);
        for(int i = 0; i < m; ++i){
            int u, v;
            scanf("%d %d", &u, &v);
            addEdge(u, v, getDis(u, v));
        }
        dis[1] = 0;
        spfa(1);
        /*for(int i = 1; i <= n; ++i)
            printf("dis[%d]:%d\n", i, dis[i]);*/
        printf("Case %d:\n", C);
        scanf("%d", &m);
        while(m--){
            int q;
            scanf("%d", &q);
            if(r[q] || dis[q] < 3 || dis[q] == INF)
                printf("?\n");
            else
                printf("%d\n", dis[q]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Never__Give_Up_/article/details/84336737