Extended Traffic

题目链接:https://vjudge.net/problem/LightOJ-1074

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:

?

题意:

给出一个有向图,假设一条边为u->v,其边权为(v-u)^3,最后有多个询问,问从1出发,到达询问目的地的花费最小为多少。当花费小于3时,输出“?”。

题解:

建图很简单,直接三方建就是了。然后从一号点跑最短路,注意用spfa判下负环,负环能够到达的点也是“?”。

代码如下:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
int d[N],vis[N],c[N],a[N],check[N],fa[N],head[N];
int t,n,m,S;
int qp(int x){
return x*x*x;
}
struct Edge{
int u,v,w,next;
}e[N*N<<];
int tot;
void adde(int u,int v,int w){
e[tot].u=u;e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
int spfa(int s){
queue <int> q;
memset(d,INF,sizeof(d));memset(fa,-,sizeof(fa));
memset(vis,,sizeof(vis));memset(c,,sizeof(c));
q.push(s);vis[s]=;d[s]=;c[s]=;
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
if(c[u]>n){
S=u;
return -;
}
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
fa[v]=u;
if(!vis[v]){
vis[v]=;
q.push(v);
c[v]++;
}
}
}
}
return d[n];
}
void dfs(int s){
check[s]=;
for(int i=head[s];i!=-;i=e[i].next){
int v=e[i].v;
if(!check[v]) dfs(v);
}
}
int main(){
int cnt = ;
cin>>t;
while(t--){
cnt++;
cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
cin>>m;
memset(check,,sizeof(check));
memset(head,-,sizeof(head));tot=;
for(int i=;i<=m;i++){
int u,v;
cin>>u>>v;
adde(u,v,qp(a[v]-a[u]));
}
int flag = spfa();
if(flag==-){
memset(check,,sizeof(check));
dfs(S);
}
int q;
cin>>q;
cout<<"Case "<<cnt<<":"<<endl;
for(int i=;i<=q;i++){
int x;
cin>>x;
if(flag==- && check[x]) cout<<"?"<<endl;
else if(d[x]< || d[x]==INF) cout<<"?"<<endl;
else cout<<d[x]<<endl;
}
}
return ;
}

Light OJ 1074:Extended Traffic(spfa判负环)的更多相关文章

  1. LightOJ 1074 Extended Traffic SPFA 消负环

    分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...

  2. LightOJ - 1074 Extended Traffic(标记负环)

    题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市u到另一个城市v的时间为:(au-av)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...

  3. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  4. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  5. spfa判负环

    bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...

  6. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

  7. 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)

    传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...

  8. BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划

    BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...

  9. [P1768]天路(分数规划+SPFA判负环)

    题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...

随机推荐

  1. sorted() ,filter() ,map()的用法

    sorted() 排序函数. 语法: sorted(Iterable, key=None, reverse=False) Iterable: 可迭代对象 key: 排序规则(排序函数), 在sorte ...

  2. Python学习之魔法方法

    Python中会看到前后都加双下划线的函数名,例如 __init__(self),这类写法在Python中具有特殊的含义.如果对象使用了这类方法中的某一个,那么这个方法将会在特殊的情况下被执行,然而几 ...

  3. 第三章 最简单的C程序设计——顺序程序设计

    一.数据的表现形式及其运算 1.常量和变量 在计算机高级语言中,数据有两种表现形式:常量和变量. 1.1.常量 在程序运行过程中,其值不能被改变的量称为常量.如:5,6,32,0.111. 数值常量就 ...

  4. model的index无限次数执行导致stackOverFlow

    model的index无限次数执行导致stackOverFlow

  5. java集合浅谈(一)

    一.类库结构图概览 容器对象仅能持有对象引用(对象的指针),而不是Copy对象信息,从网上搜得几张Java中集合类库的结构图,如下所示: 二.解说Collection 2.1 Collection ( ...

  6. 【APUE】Chapter13 Daemon Processes

    这章节内容比较紧凑,主要有5部分: 1. 守护进程的特点 2. 守护进程的构造步骤及原理. 3. 守护进程示例:系统日志守护进程服务syslogd的相关函数. 4. Singe-Instance 守护 ...

  7. fiddler抓包工具的基本使用

    fiddler是基于C#的HTTP抓包工具. fiddler的原理: fiddler是http代理服务器,它会抓取浏览器向服务器发送的HTTP请求,然后在将该请求发送到服务器.再获取从服务器返回的请求 ...

  8. C++学习007-使用exit退出进程

    使用exit可以实现退出当前进程. 如下 在程序接收到一个字符后,就退出进程 编写环境 vs2015 int main() { int a = 10, b = 20; std::cout <&l ...

  9. 解决Unbuntu终端菱形乱码问题

    原因:安装时为了学习方便选择中文安装,其字符编码相关配置如下(在/etc/default/locale中) LANG="Zh_CN.UTF-8 "LANGUAGE="zh ...

  10. Hadoop伪分布式集群

    一.HDFS伪分布式环境搭建 Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时, ...