[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 过路费
题面: 跟所有人一样,农夫约翰以着宁教我负天下牛,休教天下牛负我(原文:宁我负人,休教人负我)的伟大精神,日日夜夜苦思生财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走, ...
随机推荐
- 【数学】codeforces B. The Golden Age
http://codeforces.com/contest/813/problem/B [题意] 满足n=x^a+y^b的数字为不幸运数字,a,b都是非负整数: 求闭区间[l,r]上的最长的连续幸运数 ...
- 1027 stl
#include<stdio.h> #include<queue> using namespace std; int main() { int i,n,m,j,k,a[100 ...
- [NOIP1998] 提高组 洛谷P1013 进制位
题目描述 著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母代表数字. 例如: L K V E L L K V E K K V E KL V V E KL KK E E K ...
- centos 中找不到 php-config
两种解决方式: 1.CentOS6 PHP extension install: Cannot find php-config. Please use --with-php-config=PATH 2 ...
- zoj4028 LIS
差分约束瞎搞一下,话说这个数据不知道怎么回事,我的图按道理而言最多只有4n条边,开5n还不够??必须6n?? 约束条件首先根据f函数可建立两点之间的约束,不妨设d[i]为i到0的距离,则对于f[i] ...
- androidstudio Cannot resolve symbol 'xxx'
Android Studio 无法识别同一个 package 里的其他类,将其显示为红色,但是 compile 没有问题.鼠标放上去后显示 “Cannot resolve symbol XXX”,重启 ...
- Effective Java Profiling With Open Source Tools
https://www.infoq.com/articles/java-profiling-with-open-source
- Building clang on RedHat
http://btorpey.github.io/blog/2015/01/02/building-clang/ clang is a great compiler, with a boatload ...
- Arcgis栅格时序地图制作---时间轴动态展示多期影像
转自原文 Arcgis栅格时序地图制作---时间轴动态展示多期影像 效果如何???满意您go on,不满意咱 say goodbye··· 题外话: 为了在这里动态展示下制作结果,也是费了老劲了,转换 ...
- FIREDAC驱动ORACLE的配置
1)部署中间件所在的机器必须安装OCI 2)verdorlib,指定OCI所在路径