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大神商量后,这道猥琐的题目终于出现在本次试题上了 ...
随机推荐
- struts2架构网站漏洞修复详情与利用漏洞修复方案
struts2从开发出来到现在,很多互联网企业,公司,平台都在使用apache struts2系统来开发网站,以及应用系统,这几年来因为使用较多,被攻击者挖掘出来的struts2漏洞也越来越,从最一开 ...
- JavaScript 对引擎、运行时、调用堆栈的概述理解
JavaScript 对引擎.运行时.调用堆栈的概述理解 随着JavaScript越来越流行,越来越多的团队广泛的把JavaScript应用到前端.后台.hybrid 应用.嵌入式等等领域. 这篇文 ...
- powerpoint教程资料,PPT的
Powerpoint,是微软公司设计的演示文稿软件,利用Powerpoint不仅可以创建演示文稿,还可以在互联网上召开面对面会议.远程会议或在网上给观众展示演示文稿,掌握利用PowerPoint是一项 ...
- 生産管理(PP)
伝票系 製造指図 マスタ系 生産資源/治工具 作業区 能力 作業手順 作業バージョン 作業記録 需要予測プロファイル 計画手配 MRP レシピ その他 カスタマイズ系 BOM関連 製造指図確認 伝票系 ...
- java反射操作类方法与属性
package com.fanshe.test; public class User { private int age; private String email; private String u ...
- 让Dreamweaver支持cshtml (MVC Razor环境)
介绍:让Dreamweaver支持cshtml 正文: 如题,刚才搜了很久,都搜不到答案,幸好得到“包大人”(同事)的帮助,才得以解决. DW支持很多文件类型的代码提示,可是类型太多,不可能全部都有, ...
- NodeJS微信公众平台开发
微信是手机用户必备的App,微信最开始只是作为社交通讯应用供用户使用,但随着用户量不断的增加,微信的公众号在微信上表现出来了它强大的一面,微信公众平台具有四大优势:1.平台更加稳固:2.用户关系更加平 ...
- MySQL☞order by与distinct
asc(升序,默认值)/desc(降序) 1.根据某一列的列值进行升序或者降序操作. select 列名 别名 from 表名 order by 列名 asc/desc 2.根据多个列值进行排序 s ...
- PHP实现字节数Byte转换为KB、MB、GB、TB
function getFilesize($num) { $p = 0; $format = 'bytes'; if( $num > 0 && $num < 1024 ) ...
- 以太坊solidity编程常见错误(不定期更新)
1.报错: Expected token Semicolon got 'eth_compileSolidity' funtion setFunder(uint _u,uint _amount){ 解决 ...