题目链接: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的更多相关文章

  1. Extended Traffic LightOJ - 1074 spfa判断负环

    //判断负环 在负环内的城市输出? #include <iostream> #include <queue> #include <cstdio> #include ...

  2. Extended Traffic LightOJ - 1074 (经典SPFA问题)

    题目大意:每一个城市都有一定的繁荣度,然后给出m条有向边i->j,定义这条边的权值为pow(arr[j]-arr[i],3),然后给你q个询问,每个询问输入一个x. 然后问你点1到x的距离,如果 ...

  3. LightOJ 1074 - Extended Traffic (SPFA)

    http://lightoj.com/volume_showproblem.php?problem=1074 1074 - Extended Traffic   PDF (English) Stati ...

  4. LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)

    Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...

  5. Light OJ 1074:Extended Traffic(spfa判负环)

    Extended Traffic 题目链接:https://vjudge.net/problem/LightOJ-1074 Description: Dhaka city is getting cro ...

  6. lightoj 1074 spfa判断负环

     Extended Traffic Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Sub ...

  7. LightOj 1074 Extended Traffic (spfa+负权环)

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1074 题目大意: 有一个大城市有n个十字交叉口,有m条路,城市十分拥挤,因此每一个路 ...

  8. lightoj 1074 - Extended Traffic(spfa+负环判断)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1074 题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市I到另一个城市J ...

  9. SPFA(负环) LightOJ 1074 Extended Traffic

    题目传送门 题意:收过路费.如果最后的收费小于3或不能达到,输出'?'.否则输出到n点最小的过路费 分析:关键权值可为负,如果碰到负环是,小于3的约束条件不够,那么在得知有负环时,把这个环的点都标记下 ...

随机推荐

  1. [LeetCode] 662. Maximum Width of Binary Tree 二叉树的最大宽度

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

  2. JavaScript (JS)常用方法

    被按的按键的 unicode 是? <html> <head> <script type="text/javascript"> function ...

  3. PDMan-2.1.0 正式发布:用心开源,免费的国产数据库建模工具 PowerDesigner

    PDMan是一款开源免费的数据库模型建模工具,支持Windows,Mac,Linux等操作系统,是PowerDesigner之外,更好的免费的替代方案.他具有颜值高,使用简单的特点.包含数据库建模,灵 ...

  4. 大话设计模式Python实现-代理模式

    代理模式(Proxy Pattern):为其他对象提供一种代理以控制对这个对象的访问 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = ...

  5. Maven依赖以及项目创建

    目录: 1. Maven依赖.Eclipse中使用Maven.生命周期 1.1 Maven依赖 1.2 Eclipse中使用Maven 2. 依赖排除.通过Maven整合多个Maven 2.1 依赖排 ...

  6. PHP curl cookie不识别

    PHP curl cookie不识别 所以curl的时候别用setcookie 用了也没用

  7. 记一次错误排查,主要问题是跨平台文件中换行符(CRLF, LF)和垃圾字符( Caret Notation)

    笔者测试SPEC14的workload的时候,需要自定义workload,又需要在Windows和Linux平台上都要测试,所以就遇到了这么个问题:测试工具报错,但是报出来的错误信息又跟错误对不上. ...

  8. Installing Google Chrome in Linux(RedHat Enterprise Linux 7)

    # wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm # yum -y install r ...

  9. Redis学习之对象系统源码分析

    背景知识: Redis并没有直接使用sds,双端链表,字典,压缩列表,跳表等这些数据结构来直接实现键值对数据库,而是基于这些对象创建了一个对象系统,这个对象系统包含5个对象:字符串对象,列表对象,哈希 ...

  10. Intellij IDEA的安装教程

    一.下载安装 1.打开官网:http://www.jetbrains.com/idea/,点击页面中的“DOWNLOAD” 2.根据自己的需要选择下载的IntelliJ IDEA版本,此处我的电脑是W ...