[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 过路费
题面: 跟所有人一样,农夫约翰以着宁教我负天下牛,休教天下牛负我(原文:宁我负人,休教人负我)的伟大精神,日日夜夜苦思生财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走, ...
随机推荐
- hexo干货系列:(二)hexo主题下载及配置
前言 上一篇文章介绍了hexo+gitHub简易搭建属于自己的个人独立博客,但是主题是默认的landscape,略显简单,今天的教程推荐Jacman主题. Jacman是一款为Hexo打造的一款扁平化 ...
- FZU1004-Number Triangle经典动归题,核心思路及代码优化
Problem 1004 Number Triangle Accept: 2230 Submit: 5895Time Limit: 1000 mSec Memory Limit : 327 ...
- HDU1272 迷宫通路数
Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该 ...
- 【dp】D. Caesar's Legions
https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/D [题意]给定n1个A,n2个B,排成一排,要求A最多能连续k1个紧挨着,B最多 ...
- hdu 1565 状态压缩dp
#include<stdio.h> #include<string.h> int Max(int a,int b) { return a>b?a:b; } int dp] ...
- mybatis使用步骤
1.创建config.xml文件.设置环境.数据源等: 2.设置mapper.xml文件.写sql:下面图中的resultType属性经常会替换为resultMap,不过需要加入<resultM ...
- Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]
传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...
- 51nod 1907(多项式乘法启发式合并)
题目: 分析: 对于一个确定的生成子图,很明显是在一个连通块上走,走完了再跳到另一个连通块上,假设连通块个数为cnt,那么答案一定是$min(a_{cnt-1},a_cnt,..,a_{n-1})$ ...
- springboot 第一个程序
idea --> new project --> 选择Spirng Initializr --> next 傻瓜式操作 --> 添加web依赖 项目基本结构: 创建contr ...
- 【c++】【转】结构体字节对齐
http://www.cnblogs.com/heyonggang/archive/2012/12/11/2812304.html