[Usaco2009 Dec] 过路费
[题目链接]
https://www.luogu.org/problemnew/show/P2966
[算法]
SPFA最短路
时间复杂度 : O(N ^ 2)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 510
#define MAXM 10010
typedef long long LL;
const int INF = 1e9; struct edge
{
int to , w , nxt;
} e[MAXM << ]; int n , m , q , tot;
int a[MAXN] , head[MAXN];
LL dist[MAXN][MAXN];
bool inq[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u , int v , int w)
{
++tot;
e[tot] = (edge){v , w , head[u]};
head[u] = tot;
}
inline void spfa(int id , int s)
{
queue< int > que;
memset(inq , false , sizeof(inq));
que.push(s);
while (!que.empty())
{
int cur = que.front();
que.pop();
inq[cur] = false;
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (dist[id][cur] + w < dist[id][v] && a[v] <= a[id])
{
dist[id][v] = dist[id][cur] + w;
if (!inq[v])
{
inq[v] = true;
que.push(v);
}
}
}
}
} int main()
{ read(n); read(m); read(q);
for (int i = ; i <= n; i++) read(a[i]);
for (int i = ; i <= m; i++)
{
int u , v , w;
read(u); read(v); read(w);
addedge(u , v , w);
addedge(v , u , w);
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
dist[i][j] = INF;
}
}
for (int i = ; i <= n; i++)
{
dist[i][i] = ;
spfa(i , i);
}
while (q--)
{
int x , y;
read(x); read(y);
LL ans = INF;
for (int i = ; i <= n; i++) chkmin(ans , dist[i][x] + dist[i][y] + a[i]);
printf("%lld\n" , ans);
} return ;
}
[Usaco2009 Dec] 过路费的更多相关文章
- 1774: [Usaco2009 Dec]Toll 过路费
1774: [Usaco2009 Dec]Toll 过路费 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 263 Solved: 154[Submit ...
- BZOJ_1774_[Usaco2009 Dec]Toll 过路费_floyd
BZOJ_1774_[Usaco2009 Dec]Toll 过路费_floyd 题意: 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一 ...
- BZOJ3412: [Usaco2009 Dec]Music Notes乐谱
3412: [Usaco2009 Dec]Music Notes乐谱 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 35 Solved: 30[Sub ...
- BZOJ3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者
3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 47 Solve ...
- 3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者
3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 71 Solve ...
- [bzoj1775][Usaco2009 Dec]Vidgame 电视游戏问题_背包dp
1775: [Usaco2009 Dec]Vidgame 电视游戏问题 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1775 题解: 发 ...
- Floyd | | jzoj[1218] | | [Usaco2009 Dec]Toll 过路费 | | BZOJ 1774 | | 我也不知道该怎么写
写在前面:老师说这一道题是神题,事实上确实如此,主要是考察对Floyd的理解 ******************************题目.txt************************* ...
- [bzoj 1774][Usaco2009 Dec]Toll 过路费
题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...
- [Usaco2009 Dec]Toll 过路费
题面: 跟所有人一样,农夫约翰以着宁教我负天下牛,休教天下牛负我(原文:宁我负人,休教人负我)的伟大精神,日日夜夜苦思生财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走, ...
随机推荐
- zoj 2812
Quicksum Time Limit: 2 Seconds Memory Limit: 65536 KB A checksum is an algorithm that scans a p ...
- CF671D:Roads in Yusland
n<=300000个点的树,给m<=300000条带权路径(ui,vi,保证vi是ui的祖先)求覆盖整棵树每条边的最小权和. 好题好姿势!直观的看到可以树形DP,f[i]表示把点i包括它爸 ...
- POJ 1470 Closest Common Ancestors【LCA Tarjan】
题目链接: http://poj.org/problem?id=1470 题意: 给定若干有向边,构成有根数,给定若干查询,求每个查询的结点的LCA出现次数. 分析: 还是很裸的tarjan的LCA. ...
- 碧砚适合佳能328 4452 ICD520 4472 4450 硒鼓4700一体机墨盒4770
- eclipse导入maven工程步骤
转自:http://jingyan.baidu.com/article/cbf0e500a6e3252eaa2893c1.html 感谢作者 步骤一 : 选择 “Import”操作 有两个途径可以选择 ...
- 查看linux接口进出口流量的命令;linux 网络监控;流量监控
1.nload,左右键切换网卡 2.sudo iftop 3.sudo iptraf 按连接/端口查看流量 4.sudo nethogs: 按进程查看流量占用 5.ss: 连接查看工具 6.dstat ...
- python字符串连接方法效率比较
方法1:直接通过加号(+)操作符连接 1 website = 'python' + 'tab' + '.com' 方法2:join方法 1 2 listStr = ['python', 'tab', ...
- 【python】Python的字典get方法:从字典中获取一个值
转自: http://blog.sina.com.cn/s/blog_6be89284010183xm.html
- Deepin-文件目录介绍
请参见这篇文件:来自一个强大的网站 我主要介绍的就是: 下面所列文件,全部添加进了path目录(Linux查找命令,请参见man.linux,无论是find 或者是 which等) Deepin默认可 ...
- Nerv --- React IE8 兼容方案
创建项目 创建一个目录,使用npm快速初始化 $ mkdir my-project && npm init -y 安装依赖 安装webpack以及babel $ npm install ...