http://www.lydsy.com/JudgeOnline/problem.php?id=2015

这种水题真没啥好说的。。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <queue>
  8. using namespace std;
  9. #define rep(i, n) for(int i=0; i<(n); ++i)
  10. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  11. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  12. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  13. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  14. #define CC(i,a) memset(i,a,sizeof(i))
  15. #define read(a) a=getint()
  16. #define print(a) printf("%d", a)
  17. #define dbg(x) cout << #x << " = " << x << endl
  18. #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
  19. inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
  20. inline const int max(const int &a, const int &b) { return a>b?a:b; }
  21. inline const int min(const int &a, const int &b) { return a<b?a:b; }
  22.  
  23. const int N=55005, Q=N*10, oo=~0u>>2;
  24. int q[Q], front, tail, d[N], vis[N], ihead[N], cnt, n, x, m;
  25. struct ED { int to, w, next; }e[Q];
  26. void add(int u, int v, int w) {
  27. e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
  28. e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].w=w;
  29. }
  30. void spfa() {
  31. q[tail++]=1;
  32. for1(i, 2, n) d[i]=oo;
  33. vis[1]=1;
  34. while(front!=tail) {
  35. int v, u=q[front++]; if(front==Q) front=0; vis[u]=0;
  36. for(int i=ihead[u]; i; i=e[i].next) if(d[v=e[i].to]>d[u]+e[i].w) {
  37. d[v]=d[u]+e[i].w;
  38. if(!vis[v]) {
  39. vis[v]=1;
  40. q[tail++]=v; if(tail==Q) tail=0;
  41. }
  42. }
  43. }
  44. }
  45.  
  46. int main() {
  47. read(n); read(m); read(x);
  48. for1(i, 1, m) {
  49. int u=getint(), v=getint(), w=getint();
  50. add(u, v, w);
  51. }
  52. spfa();
  53. for1(i, 1, x) {
  54. int u=getint(), v=getint();
  55. printf("%d\n", d[u]+d[v]);
  56. }
  57. return 0;
  58. }

【BZOJ】2015: [Usaco2010 Feb]Chocolate Giving(spfa)的更多相关文章

  1. 【BZOJ】2014: [Usaco2010 Feb]Chocolate Buying(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2014 这应该是显然的贪心吧,先排序,然后按花费取 #include <cstdio> # ...

  2. 【BZOJ】1631: [Usaco2007 Feb]Cow Party(dijkstra)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1631 看到m<=100000果断用dij(可是好像dij比spfa还慢了在这里?)//upd: ...

  3. 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...

  4. 【BZOJ】2021: [Usaco2010 Jan]Cheese Towers(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2021 噗,自己太弱想不到. 原来是2次背包. 由于只要有一个大于k的高度的,而且这个必须放在最顶,那 ...

  5. 【BZOJ】2100: [Usaco2010 Dec]Apple Delivery(spfa+优化)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2100 这题我要吐血啊 我交了不下10次tle.. 噗 果然是写挫了. 一开始没加spfa优化果断t ...

  6. 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...

  7. 【BZOJ】3538: [Usaco2014 Open]Dueling GPS(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3538 题意不要理解错QAQ,是说当前边(u,v)且u到n的最短距离中包含这条边,那么这条边就不警告. ...

  8. 【BZOJ】2019: [Usaco2009 Nov]找工作(spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2019 spfa裸题.....将飞机场的费用变成负,然后spfa找正环就行了 #include < ...

  9. BZOJ 2015: [Usaco2010 Feb]Chocolate Giving( 最短路 )

    裸最短路.. ------------------------------------------------------------------------------------ #include ...

随机推荐

  1. linux中添加开机自启服务的方法

    往文件/etc/rc.d/rc.local中追加内容即可,如: /mongodb/start_mongoDB.sh

  2. vue - 实例事件

    1.$on(在构造器外部添加事件) 2.$once(执行一次的事件) 3.$off(关闭事件) 4.$emit(事件调用) <!DOCTYPE html> <html lang=&q ...

  3. Php如何返回json数据

    php返回json,xml,JSONP等格式的数据 返回json数据: header('Content-Type:application/json; charset=utf-8'); $arr = a ...

  4. Odoo8.0安装wkhtmltopdf报表运行环境

    #下载   #注意根据操作系统选择相应版本   (旧地址)wget http://sourceforge.net/projects/wkhtmltopdf/files/0.12.1/wkhtmltox ...

  5. 简单易懂的Dart》 - Dart语言中文简明教程

    转自:https://www.blackglory.me/straightforward-dart/ Dart是Google公司发布的网络编程语言,其诞生的目的是为了让广大C类OOP程序员们克服Jav ...

  6. Eclipse项目上红叉

    整个项目可以编译通过并且运行都没问题,但是项目上有个红叉,这个问题一般是有两个原因: 1.查看项目是不是有的引用包报错 解决办法:在项目的build path 中删除不可用引用或者修正 2.项目的编译 ...

  7. Python-文件修改器

    #-*- coding: utf-8 -*- import os import sys import glob from PyQt4.QtGui import * from PyQt4.QtCore ...

  8. java基础讲解04-----数据类型和运算符

    1.java的基本数据类型 1.数值型  { 整数型   byte  , short  ,int  ,long 浮点型   float , double } 2.字符型 3.布尔型 2.运算符 1.赋 ...

  9. c# 读取文件流

    1.获取文件路径 2.编写读取路径文件信息 private string ReadFileStream(string filePath)         {             string st ...

  10. 重写spring cloud config 本地bootstrap

    在spring-cloud中使用了config-server之后,需要在client端加入bootstrap作为配置文件,其中通常包含如下: spring.application.name=ms-as ...