zoukankan      html  css  js  c++  java
  • Extended Traffic LightOJ

    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:

    ?

    题意:有n个点,每个点都有自己的值,点和点部分之间有道路,道路是单向的,道路有权值,a点到b点的权值等于(b-a)d 三次方,问从1到其他点之间的最短权值。

    输入:第一行为测试样例,每个案例都包含一个空行和一个表示结点数的整数n(1 <n≤200)下一行包含n个整数,分别表示各点的值,下一行包含一个整数m,即城市中道路的数量。接下来的m行中的每一行(每条道路一条)包含相应道路连接的两个交叉点(源,目的地)(所有道路都是单向的)。下一行包含整数q,即查询数。下一个q行每个都包含一个目标结点号。从交叉路口到另一个交叉路口最多只能有一条直达道路。

    输出:对于每种情况,请在一行中打印案例编号。然后打印q行,每个查询一行,每行包含从结点1(零点)到达给定结点的最小总收入但是,对于总收入小于3的查询,或者如果目的地无法从零点到达,则打印“?” 

    思路:相当于单向道路的图,求起点到各点的最小权,但由于是立方因此可能含有负权,所以要用SPFA算法。同时,如果存在负环的话,无论如何走没有最小权,因此与负权有关的点也是无解的,

    代码:

      1 #include <cstdio>
      2 #include <fstream>
      3 #include <algorithm>
      4 #include <cmath>
      5 #include <deque>
      6 #include <vector>
      7 #include <queue>
      8 #include <string>
      9 #include <cstring>
     10 #include <map>
     11 #include <stack>
     12 #include <set>
     13 #include <sstream>
     14 #include <iostream>
     15 #define mod 998244353
     16 #define eps 1e-6
     17 #define ll long long
     18 #define INF 0x3f3f3f3f
     19 using namespace std;
     20 
     21 struct node
     22 {
     23     int y,z,next;
     24 };
     25 //存放边的信息
     26 node no[40005];
     27 int t,m,n,q;
     28 //存放每条边的位置
     29 int head[210];
     30 //存放起点到其他点的最短距离
     31 int dis[210];
     32 //判别是否已是最短距离
     33 bool vis[210];
     34 //存放该点被循环的次数
     35 int cnt[210];
     36 //标记与负环链接的点
     37 int bj[210];
     38 //num存放点的值,w存放询问的点
     39 int num[210],w[210];
     40 //s表示边数
     41 int s;
     42 //循环将负环能达到的点全标记
     43 void dfs(int ne)
     44 {
     45     bj[ne]=1;
     46     for(int i=head[ne];i!=-1;i=no[i].next)
     47     {
     48         int v=no[i].y;
     49         //判断此点是否被标记过
     50         if(!bj[v])
     51         {
     52             dfs(v);
     53         }
     54     }
     55 }
     56 void spfa()
     57 {
     58     queue<int> qu;
     59     qu.push(1);
     60     //初始化
     61     memset(vis,0,sizeof(vis));
     62     memset(dis,INF,sizeof(dis));
     63     memset(cnt,0,sizeof(cnt));
     64     memset(bj,0,sizeof(bj));
     65     vis[1]=1;
     66     dis[1]=0;
     67     //为空时退出
     68     while(!qu.empty())
     69     {
     70         int k=qu.front();
     71         qu.pop();
     72         //将此点标记为0,重新更新与此点连接的数据
     73         vis[k]=0;
     74         //遍历与k相连接的点
     75         for(int i=head[k];i!=-1;i=no[i].next)
     76         {
     77             //nex表示与k连接的第i个点
     78             int nex= no[i].y;
     79             //如果此点与负环相连,跳过继续
     80             if(bj[nex])
     81             {
     82                 continue;
     83             }
     84             //更新最小数据
     85             if(dis[nex]>dis[k]+no[i].z)
     86             {
     87                 dis[nex]=dis[k]+no[i].z;
     88                 //如果没被标记
     89                 if(!vis[nex])
     90                 {
     91                     //入队
     92                     qu.push(nex);
     93                     vis[nex]=1;
     94                     //循环次数+1
     95                     cnt[nex]++;
     96                     //如果循环次数大于n,表示有负环
     97                     if(cnt[nex]>n)
     98                     {
     99                         dfs(nex);
    100                     }
    101                 }
    102             }
    103         }
    104     }
    105 
    106 }
    107 int main()
    108 {
    109     scanf("%d",&t);
    110     int ans=1;
    111     while(t--)
    112     {
    113         s=1;
    114         //输入点数
    115         scanf("%d",&n);
    116         //输入点值
    117         for(int i=1;i<=n;i++)
    118         {
    119             scanf("%d",&num[i]);
    120         }
    121         //输入边数
    122         scanf("%d",&m);
    123         int a,b;
    124         //初始化
    125         memset(head,-1,sizeof(head));
    126         //记录边的信息
    127         for(int i=1;i<=m;i++)
    128         {
    129             scanf("%d %d",&a,&b);
    130             no[s].y=b;
    131             no[s].z=(int)pow((num[b]-num[a]),3);
    132             no[s].next=head[a];
    133             head[a]=s++;
    134         }
    135         spfa();
    136         scanf("%d",&q);
    137         //输入提问的点
    138         for(int i=1;i<=q;i++)
    139         {
    140             scanf("%d",&w[i]);
    141         }
    142         printf("Case %d:
    ",ans++);
    143         for(int i=1;i<=q;i++)
    144         {
    145             //判断询问的点,如果小于3或无解或有负环则都输出“?”
    146             if(bj[w[i]]||dis[w[i]]<3||dis[w[i]]>=INF)
    147             {
    148                 printf("?
    ");
    149             }
    150             else
    151             {
    152                 printf("%d
    ",dis[w[i]]);
    153             }
    154         }
    155     }
    156 }
  • 相关阅读:
    2019年技能学习计划
    EVM项目管理
    常用LINQ关键字用法汇总
    如何让Enum枚举实现异或操作
    使用COM打开Excel文档注意事项
    C#使用NPOI读写Excel的注意事项
    应用国际化多语言化实现方法
    DLL简单分析与调用方法
    C#读写Excel实践笔记
    Vue基础开发笔记
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11559741.html
Copyright © 2011-2022 走看看