http://lightoj.com/volume_showproblem.php?problem=1074

1074 - Extended Traffic
Time Limit: 2 second(s) Memory Limit: 32 MB

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

Output for 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

Case 1:

3

4

Case 2:

?

SPFA 判断负环,标记负环可达的。

 /* ***********************************************
Author :kuangbin
Created Time :2013-10-2 18:08:34
File Name :E:\2013ACM练习\专题强化训练\图论一\LightOJ1074.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int v,cost;
Edge(int _v = , int _cost = )
{
v = _v;
cost = _cost;
}
};
vector<Edge>E[MAXN];
void addedge(int u,int v,int w)
{
E[u].push_back(Edge(v,w));
} bool vis[MAXN];
int cnt[MAXN];
int dist[MAXN]; bool cir[MAXN];
void dfs(int u)
{
cir[u] = true;
for(int i = ;i < E[u].size();i++)
if(!cir[E[u][i].v])
dfs(E[u][i].v);
} void SPFA(int start,int n)
{
memset(vis,false,sizeof(vis));
for(int i = ;i <= n;i++)
dist[i] = INF;
vis[start] = true;
dist[start] = ;
queue<int>que;
while(!que.empty())que.pop();
que.push(start);
memset(cnt,,sizeof(cnt));
cnt[start] = ;
memset(cir,false,sizeof(cir));
while(!que.empty())
{
int u = que.front();
que.pop();
vis[u] = false;
for(int i = ;i < E[u].size();i++)
{
int v = E[u][i].v;
if(cir[v])continue;
if(dist[v] > dist[u] + E[u][i].cost)
{
dist[v] = dist[u] + E[u][i].cost;
if(!vis[v])
{
vis[v] = true;
que.push(v);
cnt[v]++;
if(cnt[v] > n)
dfs(v);
}
}
}
}
}
int a[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int iCase = ;
scanf("%d",&T);
while(T--)
{
iCase++;
int n;
scanf("%d",&n);
for(int i = ;i <= n;i++)
scanf("%d",&a[i]);
int m;
int u,v;
for(int i = ;i <= n;i++)
E[i].clear();
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&u,&v);
addedge(u,v,(a[v]-a[u])*(a[v]-a[u])*(a[v]-a[u]));
}
SPFA(,n);
printf("Case %d:\n",iCase);
scanf("%d",&m);
while(m--)
{
scanf("%d",&u);
if(cir[u] || dist[u] < || dist[u] == INF)
printf("?\n");
else printf("%d\n",dist[u]);
}
}
return ;
}

LightOJ 1074 - Extended Traffic (SPFA)的更多相关文章

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

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

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

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

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

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

  4. LightOJ 1074 - Extended Traffic 【SPFA】(经典)

    <题目链接> 题目大意:有n个城市,每一个城市有一个拥挤度Ai,从一个城市I到另一个城市J的时间为:(A(v)-A(u))^3.问从第一个城市到达第k个城市所花的时间,如果不能到达,或者时 ...

  5. LightOJ - 1074 Extended Traffic(标记负环)

    题意:有n个城市,每一个城市有一个拥挤度ai,从一个城市u到另一个城市v的时间为:(au-av)^3,存在负环.问从第一个城市到达第k个城市所话的时间,如果不能到达,或者时间小于3输出?否则输出所花的 ...

  6. (简单) LightOJ 1074 Extended Traffic,SPFA+负环。

    Description Dhaka city is getting crowded and noisy day by day. Certain roads always remain blocked ...

  7. LightOJ - 1074 Extended Traffic (SPFA+负环)

    题意:N个点,分别有属于自己的N个busyness(简称b),两点间若有边,则边权为(ub-vb)^3.Q个查询,问从点1到该点的距离为多少. 分析:既然是差的三次方,那么可能有负边权的存在,自然有可 ...

  8. LightOJ 1074 Extended Traffic(spfa+dfs标记负环上的点)

    题目链接:https://cn.vjudge.net/contest/189021#problem/O 题目大意:有n个站点,每个站点都有一个busyness,从站点A到站点B的花费为(busynes ...

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

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

随机推荐

  1. 20155338 2016-2017-2 《Java程序设计》第7周学习总结

    20155338 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 本周学习了第十二章和第十三章的内容,我重点学习了第十三章时间与日期的相关内容. 时间的度量: ...

  2. mongoDB - 日常操作四

    python 使用 mongodb easy_install pymongo # 安装(python2.+) import pymongo connection=pymongo.Connection( ...

  3. TrID文件类型识别linux版

    读取文件头根据特征码进行文件类型匹配. 官方:http://mark0.net/soft-trid-e.html windows版本小工具:FileAnalysis 以下是linux版本 wget h ...

  4. 关于sru源码class Model的parameters

    class Model(nn.Module): def __init__(self, words, args): super(Model, self).__init__() self.args = a ...

  5. Redis的五大数据类型

    1.String(字符串) String是Redis最基本的类型,一个Key对应一个Value. String类型是二进制安全的,意思是Redis的String可以包含任何数据,比如jpg图片或者序列 ...

  6. 基于滑动窗口协议写的程序(UDP实现) .

    正好有一个大作业关于用socket实现滑动窗口协议,所以写了一个,模拟接收方与发送方窗口都是2,用两个线程实现. 下面是代码,注释的比较详细了. socket_udp.h #include<st ...

  7. 二十三、springboot之session共享

    通过redis实现session共享 SpringBoot集成springsession 1.引入依赖(gradle方式) dependencies { compile('org.springfram ...

  8. Web前端开发规范文档你需要知道的事

    Web前端开发规范文档你需要知道的事 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必须按本文档规范进 ...

  9. 通过微信Android和iOS版,看两大系统的差异

    由于设计师或者产品经理使用的移动设备大部分是iPhone,所以在做设计时,容易忽略Android和iOS的差异,按照自己的使用习惯进行设计,导致大部分设计师或产品经理做出的设计都是基于iOS规范或习惯 ...

  10. Windows 7 x64环境下SDK Manager闪退的解决方法

    1.下载并解压:http://dl.google.com/android/adt/adt-bundle-windows-x86_64-20140702.zip 2.安装JDK,否则SDK Manage ...