Extended Traffic LightOJ - 1074
题目链接:https://vjudge.net/problem/LightOJ-1074
思路:(busyness of destination - busyness of source)3 可能会是负值,那么可能出现负环,
需要SFPA来解决,我们需要把负环中的点能到达其他点都标记为‘?’点,因为这些点在有限次循环后
会变成负值,一定小于3,可以用dfs来遍历能经过的所有点。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std; typedef long long LL;
#define inf 1e9
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = ;
int head[N];
int tot[N];
bool vis[N];
int tmp[N];
bool bad[N];
int dis[N];
stack<int> sta;
int cnt;
int bus[N];
int n,m; struct Edge{
int to;
int w;
int next;
}e[N * N]; void add(int u,int v,int w){
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
}
//遍历所有能到达的点
void dfs(int u){ bad[u] = true; for(int o = head[u]; ~o; o = e[o].next){
if(!bad[e[o].to])
dfs(e[o].to);
}
} void SPFA(){ while(!sta.empty()) sta.pop();
rep(i,,n) vis[i] = false;
rep(i,,n) bad[i] = false;
rep(i,,n) dis[i] = inf;
rep(i,,n) tot[i] = ;
dis[] = ;
sta.push(); int u,v,w;
while(!sta.empty()){
u = sta.top();
sta.pop();
if(bad[u]) continue; //‘?’点
vis[u] = false; for(int o = head[u]; ~o; o = e[o].next){
w = e[o].w;
v = e[o].to; if(!bad[v]/* '?'点 */ && dis[u] + w < dis[v]){
dis[v] = dis[u] + w;
// printf("this is dis[%d] = %d\n",v,dis[v]);
if(!vis[v]){
if(++tot[v] > n) dfs(v);
else{
vis[v] = true;
sta.push(v);
}
}
}
}
}
} void print(int o){ int tot;
scanf("%d",&tot);
rep(i,,tot){
scanf("%d",&tmp[i]);
}
printf("Case %d:\n",o);
rep(i,,tot){
if(bad[tmp[i]] || dis[tmp[i]] < || dis[tmp[i]] == inf) printf("?\n");
else printf("%d\n",dis[tmp[i]]);
}
} int main(){ int T;
scanf("%d",&T); int u,v;
rep(o,,T){
scanf("%d",&n);
rep(i,,n){
scanf("%d",&bus[i]);
} rep(i,,n) head[i] = -;
cnt = ; scanf("%d",&m);
rep(i,,m){
scanf("%d%d",&u,&v);
add(u,v,(bus[v] - bus[u])*(bus[v] - bus[u])*(bus[v] - bus[u]));
} SPFA();
print(o);
} getchar(); getchar();
return ;
}
Extended Traffic LightOJ - 1074的更多相关文章
- Extended Traffic LightOJ - 1074 spfa判断负环
//判断负环 在负环内的城市输出? #include <iostream> #include <queue> #include <cstdio> #include ...
- Extended Traffic LightOJ - 1074 (经典SPFA问题)
题目大意:每一个城市都有一定的繁荣度,然后给出m条有向边i->j,定义这条边的权值为pow(arr[j]-arr[i],3),然后给你q个询问,每个询问输入一个x. 然后问你点1到x的距离,如果 ...
- LightOJ 1074 - Extended Traffic (SPFA)
http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic PDF (English) Stati ...
- LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)
Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...
- Light OJ 1074:Extended Traffic(spfa判负环)
Extended Traffic 题目链接:https://vjudge.net/problem/LightOJ-1074 Description: Dhaka city is getting cro ...
- lightoj 1074 spfa判断负环
Extended Traffic Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Sub ...
- LightOj 1074 Extended Traffic (spfa+负权环)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...
- lightoj 1074 - Extended Traffic(spfa+负环判断)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...
- SPFA(负环) LightOJ 1074 Extended Traffic
题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下 ...
随机推荐
- async和await的用法
function fn(){ return "aa"; } console.log(fn())// 如果直接写个函数return一个值,那么打印的肯定就是aa async func ...
- 公共组件及脚手架webpack模板
一.公共组件的创建和使用 前面已经学习vue组件时,了解了公共组件,但在脚手架项目中只使用过局部组件.这里是讲解全局组件如何在脚手架项目中去使用. 1.创建全局组件 在src/components/C ...
- 配置Hive 支持 JSON 存储
1.说明 hive默认使用分隔符如空格,分号,"|",制表符\t来格式化数据记录,对于复杂数据类型如json,nginx日志等,就没有办法拆分了,这时候需要更加强大的SerDe来处 ...
- java while 循环
public class Sample { public static void main(String[] args) { ; ) { System.out.print(num + " & ...
- oracle--介质恢复和实例恢复的基本概念
1.概念 REDO LOG是Oracle为确保已经提交的事务不会丢失而建立的一个机制.实际上REDO LOG的存在是为两种场景准备的,一种我们称之为实例恢复(INSTANCE RECOVERY),一种 ...
- $ is not defined与SpringMVC访问静态资源
编写前台Jquery代码时,遇到谷歌浏览器报错:Uncaught ReferenceError: $ is not defined 意味着Jquery库并没有导入到页面.这是有几种情况需要考虑: 1. ...
- [转帖]龙芯3A4000处理器实测:28nm工艺不变 性能仍可提升100%以上
龙芯3A4000处理器实测:28nm工艺不变 性能仍可提升100%以上 http://news.mydrivers.com/1/663/663122.htm 龙芯是中科院下属的计算机所研发的自主产权国 ...
- Blackbox_exporter黑盒监测
一.概述 blackbox_exporter是Prometheus 官方提供的 exporter 之一,可以提供 http.dns.tcp.icmp 的监控数据采集.Blackbox_exporter ...
- 【HTML】前台input上传限制文件类型
仅限制xls文件上传 <input id="uploadSkufile" type="file" value="批量导入" style ...
- ionic4 页面跳转传值和新页面取值
页面跳转 : <ion-row *ngFor="let item of aboutData.stockData" [routerLink]="[ '/stock-d ...