• C-TT的美梦


    题意:

    这一晚,TT 做了个美梦!
    
    在梦中,TT 的愿望成真了,他成为了喵星的统领!喵星上有 N 个商业城市,编号 1 ~ N,其中 1 号城市是 TT 所在的城市,即首都。
    
    喵星上共有 M 条有向道路供商业城市相互往来。但是随着喵星商业的日渐繁荣,有些道路变得非常拥挤。正在 TT 为之苦恼之时,他的魔法小猫咪提出了一个解决方案!TT 欣然接受并针对该方案颁布了一项新的政策。
    
    具体政策如下:对每一个商业城市标记一个正整数,表示其繁荣程度,当每一只喵沿道路从一个商业城市走到另一个商业城市时,TT 都会收取它们(目的地繁荣程度 - 出发地繁荣程度)^ 3 的税。
    
    TT 打算测试一下这项政策是否合理,因此他想知道从首都出发,走到其他城市至少要交多少的税,如果总金额小于 3 或者无法到达请悄咪咪地打出 '?'。 

    Input

    第一行输入 T,表明共有 T 组数据。(1 <= T <= 50)
    
    对于每一组数据,第一行输入 N,表示点的个数。(1 <= N <= 200)
    
    第二行输入 N 个整数,表示 1 ~ N 点的权值 a[i]。(0 <= a[i] <= 20)
    
    第三行输入 M,表示有向道路的条数。(0 <= M <= 100000)
    
    接下来 M 行,每行有两个整数 A B,表示存在一条 A 到 B 的有向道路。
    
    接下来给出一个整数 Q,表示询问个数。(0 <= Q <= 100000)
    
    每一次询问给出一个 P,表示求 1 号点到 P 号点的最少税费。

    Output

    每个询问输出一行,如果不可达或税费小于 3 则输出 '?'。

    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
    10
    1 2 4 4 5 6 7 8 9 10
    10
    1 2
    2 3
    3 1
    1 4
    4 5
    5 6
    6 7
    7 8
    8 9
    9 10
    2
    3 10

    Sample Output

    Case 1:
    3
    4
    Case 2:
    ?
    ?

    MY Solution:

    这道题可以抽象成求一个含负权边的单源最短路径问题。

    可用spfa算法  or  Bellman-ford 解决:

    核心:bellman-ford 在判断负环路,即权值之和小于0的环路,对于每一条边,如果dis[u]+w(u,v) < dis[v],则存在负环路,说明无法求出最短路。 

               spfa通过进入队列次数(最多N次)来判断是否存在负环。

    Code:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<queue> 
     5 using namespace std;
     6 const int INF=999999999;
     7 const int maxn = 100005;
     8 
     9 int head[100010],tot;
    10 int dis[maxn],a[maxn],cnt[maxn];
    11 bool vis1[maxn],vis2[maxn];
    12 
    13 struct edge{
    14     int to,next,w;
    15 }e[maxn]; 
    16 void init(){
    17     tot=0;
    18     memset(head,-1,sizeof(head));
    19     memset(vis1,0,sizeof(vis1));
    20     memset(vis2,0,sizeof(vis2));
    21     memset(cnt,0,sizeof(cnt));
    22 }
    23 void add(int x,int y,int w){
    24     e[++tot].to=y;
    25     e[tot].next=head[x];
    26     e[tot].w=w;
    27     head[x]=tot;
    28 }
    29 void dfs(int u){
    30     for(int i=head[u];i;i=e[i].next){
    31         if(vis1[e[i].to]==0){
    32             vis1[e[i].to]=1;
    33             dfs(e[i].to);
    34         }
    35     }
    36 }
    37 int main(){
    38     int T,N,M,A,B,Q,P,op=0;
    39     cin>>T;
    40     while(T--){
    41         op++;
    42         init();
    43         cin>>N;
    44         for(int i=1;i<=N;i++)
    45             cin>>a[i];
    46         cin>>M;
    47         while(M--){
    48             cin>>A>>B;
    49             add(A,B,pow((a[B]-a[A]),3));
    50         }
    51         
    52         for(int i=1;i<=N;i++)
    53             dis[i]=INF;
    54         dis[1]=0;
    55         queue<int> q;
    56         q.push(1);
    57         vis2[1]=1;
    58         while(!q.empty()){
    59             int data=q.front();
    60             q.pop();
    61             vis2[data]=0;
    62             for(int i=head[data];i;i=e[i].next){
    63                 if(dis[e[i].to]>dis[data]+e[i].w){
    64                     dis[e[i].to]=dis[data]+e[i].w;
    65                     cnt[e[i].to]++;
    66                     if(cnt[e[i].to]>N){
    67                         dfs(e[i].to);
    68                         continue;
    69                     }
    70                     if(!vis2[e[i].to]){
    71                         q.push(e[i].to);
    72                         vis2[e[i].to]=1;
    73                     }
    74                 }
    75             }
    76         }
    77         cout<<"Case "<<op<<":"<<endl;
    78         cin>>Q;
    79         while(Q--){
    80             cin>>P;
    81             if(dis[P]<3||vis1[P]||dis[P]== INF)
    82                 cout<<"?"<<endl;
    83             else
    84                 cout<<dis[P]<<endl;
    85         }
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    Python进阶-数据库操作
    Python进阶-数据库(MySQL)介绍与基本操作
    Python进阶-网络编程之TCP粘包
    表与表之间的关系
    基本数据类型
    mysql
    io模型
    GIL,线程池与进程池
    线程
    并发编程,进程
  • 原文地址:https://www.cnblogs.com/liuzhuan-xingyun/p/12717909.html
Copyright © 2020-2023  润新知