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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值