Light OJ 1074:Extended Traffic(spfa判负环)
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判负环)的更多相关文章
- LightOJ 1074 Extended Traffic SPFA 消负环
分析:一看就是求最短路,然后用dij,果断错了一发,发现是3次方,有可能会出现负环 然后用spfa判负环,然后标记负环所有可达的点,被标记的点答案都是“?” #include<cstdio> ...
- LightOJ - 1074 Extended Traffic(标记负环)
题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市u到另一个城市v的时间为:(au-av)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- spfa判负环
bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
- 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)
传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...
- BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划
BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...
- [P1768]天路(分数规划+SPFA判负环)
题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...
随机推荐
- python七类之字符串
字符串 一.关键字:str 字符串是不可变的可迭代的数据类型 二.方法: name = 'alex uwu sir' .title #标题 使首字母大写 只要有特殊字符隔开,才能分别认为是多个单词 ...
- [POJ 1004] Financial Management C++解题
参考:https://www.cnblogs.com/BTMaster/p/3525008.html #include <iostream> #include <cstdio> ...
- itchat和matplotlib的结合使用爬取微信信息
前几天无意中看到了一片文章,<一件有趣的事:我用 Python 爬了爬自己的微信朋友>,这篇文章写的是使用python中的itchat爬取微信中朋友的信息,其中信息包括,昵称.性别.地理位 ...
- 20145202马超《网络对抗》Exp4 恶意代码分析
20145202马超<网络对抗>Exp4 恶意代码分析 1.实验后回答问题 (1)总结一下监控一个系统通常需要监控什么.用什么来监控. 虽然这次试验的软件很好用,我承认,但是他拖慢了电脑的 ...
- 3155: Preprefix sum
3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这 ...
- Hibernate-ORM:08.Hibernate中的投影查询
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将叙述hibernate中的投影查询 一,目录: 1.解释什么是投影查询 2.返回Object单个对象 ...
- linux安装oracle远程客户端
文章参考:http://blog.csdn.net/caomiao2006/article/details/11901123 感谢博友分享O(∩_∩)O~ 安装oracle 远程客户端(一般情况下本地 ...
- loj2587 「APIO2018」铁人两项
圆方树orz,参见猫的课件(apio和wc的)以及这里那里 #include <iostream> #include <cstdio> using namespace std; ...
- 第三篇 Python执行方式和变量初始
第一个Python程序 可以打开notepad或者其他文本编辑器,输入:print("Hello Python!"),将文件保存到任意盘符下,后缀名是 .py 两种python程 ...
- Selenium Grid 环境搭建 碰到的unable to access server
1. Slenenium Grid的环境部署, 前提条件: JDK,JRE都已经安装, selenium的standalone jar包放在磁盘 执行如下命令,报错: 2. 在cmd窗口里切换到jar ...