[题目链接]

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] 过路费的更多相关文章

  1. 1774: [Usaco2009 Dec]Toll 过路费

    1774: [Usaco2009 Dec]Toll 过路费 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 263  Solved: 154[Submit ...

  2. BZOJ_1774_[Usaco2009 Dec]Toll 过路费_floyd

    BZOJ_1774_[Usaco2009 Dec]Toll 过路费_floyd 题意: 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一 ...

  3. BZOJ3412: [Usaco2009 Dec]Music Notes乐谱

    3412: [Usaco2009 Dec]Music Notes乐谱 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 35  Solved: 30[Sub ...

  4. BZOJ3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者

    3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 47  Solve ...

  5. 3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者

    3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 71  Solve ...

  6. [bzoj1775][Usaco2009 Dec]Vidgame 电视游戏问题_背包dp

    1775: [Usaco2009 Dec]Vidgame 电视游戏问题 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1775 题解: 发 ...

  7. Floyd | | jzoj[1218] | | [Usaco2009 Dec]Toll 过路费 | | BZOJ 1774 | | 我也不知道该怎么写

    写在前面:老师说这一道题是神题,事实上确实如此,主要是考察对Floyd的理解 ******************************题目.txt************************* ...

  8. [bzoj 1774][Usaco2009 Dec]Toll 过路费

    题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...

  9. [Usaco2009 Dec]Toll 过路费

    题面: 跟所有人一样,农夫约翰以着宁教我负天下牛,休教天下牛负我(原文:宁我负人,休教人负我)的伟大精神,日日夜夜苦思生财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走, ...

随机推荐

  1. ZOJ3860-Find the Spy

    Find the Spy Time Limit: 2 Seconds      Memory Limit: 65536 KB Whoooa! There is a spy in Marjar Univ ...

  2. php-fpm.conf

    [global]pid = /usr/local/php/var/run/php-fpm.piderror_log = /usr/local/php/var/log/php-fpm.loglog_le ...

  3. POJ 1276 Cash Machine 【DP】

    多重背包的模型,但一开始直接将N个物品一个一个拆,拆成01背包竟然T了!!好吧OI过后多久没看过背包问题了,翻出背包九讲看下才发现还有二进制优化一说........就是将n个物品拆成系数:1,2,4, ...

  4. 【并查集】F.find the most comfortable road

    https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/F [题意] 给定n个城市和m条带权边,q次查询,问某两个城市之间的所有路径中最大 ...

  5. 【贪心】codeforces B. Heidi and Library (medium)

    http://codeforces.com/contest/802/problem/B [题意] 有一个图书馆,刚开始没有书,最多可容纳k本书:有n天,每天会有人借一本书,当天归还:如果图书馆有这个本 ...

  6. Django 的信号 & Flask 的信号

    信号:框架内部已帮助开发者预留的可扩展的位置 一.Django 的信号 项目目录结构: django_signal |--- app01 |--- models.py |--- views.py .. ...

  7. Bzoj 2726 SDOI 任务安排

      Memory Limit: 131072KB   64bit IO Format: %lld & %llu Description 机器上有N个需要处理的任务,它们构成了一个序列.这些任务 ...

  8. C++,C程序设计入门——《高质量程序设计第4章》

    1. 连接规范 1. extern “C” 2. 一部分采用C的连接规范 #ifdef __cplusplus extern "C" { #endif #ifdef __cplus ...

  9. poj 1236+hdu2767 有向图 缩点+看度数(tarjan)

    1236题意:一个有向图,1,求至少从几个点出发可以遍历该图,2:,求至少添加多少边,使强连通.而,HDU的只有后面一问. 解;先缩点,第一问只需找所有入度为0的点即可.,第2问,max(入度为0的点 ...

  10. topcoder 650 srm div2 1000pts

    (15) 也是 DIV1 500 题意是给定 一个无向图 删去一条边以后 可不可以是完全二叉树. 细节点很多,开始做法居然求到桥去了,最近强联通写傻了. 最多1024-1个点 1024-1条边枚举 所 ...