博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lightoj1074——Extended Traffic(SPFA判断负环)
阅读量:2343 次
发布时间:2019-05-10

本文共 2863 字,大约阅读时间需要 9 分钟。

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:
?

边权值是两个城市的拥挤值相减的立方,所以很明显存在负环。SPFA判断负环的方式是统计节点出现在队列中的次数,给个限制就行。

另外这题边的数量很多,注意数组大小

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 100010#define mod 1000000007using namespace std;struct Edge{ int v,w,next;};Edge edge1[MAXN<<1];int head1[MAXN],n,m,e,vis[MAXN],dis[MAXN];int q[MAXN],p[MAXN];void add(Edge *edge,int *head,int u,int v,int w){ edge[e].v=v; edge[e].w=w; edge[e].next=head[u]; head[u]=e;}void spfa(Edge *edge,int *head,int u){ memset(vis,0,sizeof(vis)); memset(p,0,sizeof(p)); for(int i=1; i<=n; ++i) dis[i]=INF; dis[u]=0; queue
q; q.push(u); while(!q.empty()) { u=q.front(); q.pop(); vis[u]=0; p[u]++; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v,w=edge[i].w; if(w+dis[u]
=3&&dis[x]
<=n) printf("%d\n",dis[x]); else printf("?\n"); } } return 0;}

转载地址:http://vscvb.baihongyu.com/

你可能感兴趣的文章
OpenCV图像变换(仿射变换与透视变换)
查看>>
仿射变换与透视变换
查看>>
Ubuntu 16.04 上安装 CUDA 9.0 详细教程
查看>>
Verify You Have a CUDA-Capable GPU
查看>>
ROS中OpenCV的使用——人脸检测
查看>>
ROS学习笔记(1):在ROS中使用OpenCV进行简单的图象处理--原理篇
查看>>
ROS学习笔记(2):在ROS中使用OpenCV进行简单的图像处理---代码实现篇
查看>>
C语言中声明和定义详解
查看>>
ros代码中添加使用opencv库,cv::Mat和ros image之间的相互转换
查看>>
ROS 不能再详细的安装教程
查看>>
在ros底下安装opencv
查看>>
PHP页面纯静态化与伪静态化
查看>>
分享网页到微信朋友圈,显示缩略图的方法
查看>>
PHP参数类型限制
查看>>
IOS博客项目搭建-12-刷新数据-显示最新的微博数提示
查看>>
Laravel5 Markdown 编辑器使用教程
查看>>
php文件上传与下载
查看>>
Python3学习教程
查看>>
Python3学习笔记01-第一个Python程序
查看>>
Laravel5开发学生管理系统
查看>>