POJ 4046 Sightseeing
Sightseeing
This problem will be judged on PKU. Original ID: 4046
64-bit integer IO format: %lld Java class name: Main
Input
Output
Sample Input
6 7
1 2 3 4 5 6
1 2 1
2 3 2
3 4 3
4 5 4
1 5 5
2 5 2
1 4 3
5
1 4
2 3
1 5
3 5
1 6
2 1
10 20
1 2 5
1
1 2
0 0
Sample Output
7
5
8
9
-1 25
Source
#include <cstdio>
#include <queue>
#include <iostream>
#include <cstring>
#define pil pair<LL,int>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = ;
int head[maxn],tot,n,m,q,p[maxn],from[maxn*],to[maxn*];
bool done[maxn];
LL d[maxn],ans[maxn*];
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[];
void add(int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
priority_queue<pil,vector<pil >,greater<pil > >qq;
void dijkstra(int s){
while(!qq.empty()) qq.pop();
for(int i = ; i <= n; ++i){
d[i] = INF;
done[i] = false;
}
d[s] = ;
qq.push(pil(,s));
while(!qq.empty()){
int u = qq.top().second;
qq.pop();
if(done[u]) continue;
done[u] = true;
for(int i = head[u]; ~i; i = e[i].next){
if(p[e[i].to] <= p[s] && !done[e[i].to] && d[e[i].to] > d[u] + e[i].w){
d[e[i].to] = d[u] + e[i].w;
qq.push(pil(d[e[i].to],e[i].to));
}
}
}
for(int i = ; i < q; ++i)
if(d[from[i]] < INF && d[to[i]] < INF)
ans[i] = min(ans[i],d[from[i]] + d[to[i]] + p[s]);
}
int main() {
int u,v,w;
while(scanf("%d%d",&n,&m),n||m) {
for(int i = ; i <= n; ++i)
scanf("%d",p+i);
memset(head,-,sizeof head);
tot = ;
while(m--){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
scanf("%d",&q);
for(int i = ; i < q; ++i){
scanf("%d%d",from+i,to+i);
ans[i] = INF;
}
for(int i = ; i <= n; ++i) dijkstra(i);
for(int i = ; i < q; ++i)
printf("%I64d\n",ans[i] == INF?-:ans[i]);
puts("");
}
return ;
}
POJ 4046 Sightseeing的更多相关文章
- POJ 4046 Sightseeing 枚举+最短路 好题
有n个节点的m条无向边的图,节点编号为1~n 然后有点权和边权,给出q个询问,每一个询问给出2点u,v 输出u,v的最短距离 这里的最短距离规定为: u到v的路径的所有边权+u到v路径上最大的一个点权 ...
- POJ 1637 Sightseeing tour(最大流)
POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- poj 3463 Sightseeing( 最短路与次短路)
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]
嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...
- POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】
题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10306 Accepted: 3519 ...
- POJ - 3463 Sightseeing 最短路计数+次短路计数
F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
随机推荐
- Unix网络编程 高级IO套接字设置超时
我们知道.对于一个套接字的读写(read/write)操作默认是堵塞的.假设当前套接字还不可读/写,那么这个操作会一直堵塞下去,这样对于一个须要高性能的server来说,是不能接受的.所以,我们能够在 ...
- yum install mysql(转载)
linux下使用yum安装mysql 1.安装查看有没有安装过: yum list installed mysql* rpm -qa | grep mysql* 查 ...
- csharp OverflowException——超出数值范围会抛出异常
OverflowException 會在下列情況下執行階段擲回︰ 算術運算會產生作業所傳回的資料型別範圍之外的結果. 下列範例說明 OverflowException 超出範圍的乘法運算所擲回 Int ...
- string 类型的翻转
#include <string>#include <iostream>#include <stack> int main() { std::string str= ...
- c++调用DOS命令,不显示黑屏
WinExec("Cmd.exe /C md c://12", SW_HIDE); 注释:/c是什么意思,不用/C会报错 CMD [/A | /U] [/Q] [/D] [/E:O ...
- do…while语句
有些情况下,不论条件是否满足,循环过程必须至少执行一次,这时可以采用do...while语句.就像如图7.4所示登录账号一样,需要先输入密码和账户名,后进行判断:如果密码始终不正确,则循环要求用户输入 ...
- 通过ASP.NET Ajax技术模拟实现NBA比赛文字直播功能
文字直播是满足一些观看视频直播而条件不足的球迷所设定的比赛直播方式,例如在长途车上为了能够了解比赛的实时赛况但又限于流量和网速等问题,就出现了文字直播的方式.无论是拥有无线上网卡的笔记本电脑或者手机等 ...
- 联想笋尖S90(S90-t 、S90-u)解锁BootLoader
工具下载链接: http://pan.baidu.com/s/1eSgZuka 备用下载链接: http://pan.baidu.com/s/1dFKqSId 本篇教程,仅限于联想笋尖S90(S90- ...
- 向properties文件中写入信息(针对获取properties文件失败的总结)
前段时间项目需要将某个属性动态的写入项目发布路径下的properties文件中;但是实际发布时发现找不到maven项目resource路径下的project.properties文件,调试多次代码如下 ...
- SQL Server对数据进行修改
SQL Server对数据进行修改,修改数据库中的数据. auto"> <tr style="background:red"> <td>编号 ...