当前位置: 首页 > news >正文

建站域名太原seo快速排名怎么样

建站域名,太原seo快速排名怎么样,远程桌面做网站,编程软件免费下载文章目录 [USACO10FEB] Chocolate Giving S题面翻译题目描述输入格式输出格式 题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1 题意解析CODE给点思考 [USACO10FEB] Chocolate Giving S 题面翻译 题目链接:https://www.luogu.com.cn/problem/P2984 题目描…

文章目录

  • [USACO10FEB] Chocolate Giving S
    • 题面翻译
      • 题目描述
      • 输入格式
      • 输出格式
    • 题目描述
    • 输入格式
    • 输出格式
    • 样例 #1
      • 样例输入 #1
      • 样例输出 #1
  • 题意解析
  • CODE
  • 给点思考



[USACO10FEB] Chocolate Giving S

题面翻译

题目链接:https://www.luogu.com.cn/problem/P2984

题目描述

FJ 有 B B B 头奶牛 ( 1 ≤ B ≤ 25000 ) (1\le B\le 25000) (1B25000),有 N ( 2 × B ≤ N ≤ 50000 ) N(2\times B\le N\le 50000) N(2×BN50000) 个农场,编号 1 1 1 N N N,有 M ( N − 1 ≤ M ≤ 100000 ) M(N-1\le M\le 100000) M(N1M100000) 条双向边,第 i i i 条边连接农场 R i R_i Ri S i ( 1 ≤ R i ≤ N , 1 ≤ S i ≤ N ) S_i(1\le R_i\le N, 1\le S_i\le N) Si(1RiN,1SiN),该边的长度是 L i ( 1 ≤ L i ≤ 2000 ) L_i(1\le L_i\le 2000) Li(1Li2000)。居住在农场 P i P_i Pi 的奶牛 A ( 1 ≤ P i ≤ N ) (1\le P_i\le N) (1PiN),想送一份新年礼物给居住在农场 Q i ( 1 ≤ Q i ≤ N ) Q_i(1\le Q_i\le N) Qi(1QiN) 的奶牛 B,但是奶牛 A 必须先到 FJ(居住在编号 1 1 1 的农场)那里取礼物,然后再送给奶牛 B。你的任务是:奶牛 A 至少需要走多远的路程?

输入格式

  • 第一行三个整数 N , M , B N,M,B N,M,B

  • 2 2 2 M + 1 M+1 M+1 行,每行 3 3 3 个整数 R i , S i , L i R_i,S_i,L_i Ri,Si,Li

  • M + 2 M+2 M+2 M + B + 1 M+B+1 M+B+1 行,进行 B B B 次询问,每行 2 2 2 个整数 P i , Q i P_i ,Q_i Pi,Qi

输出格式

每次询问输出一个整数,即答案。

题目描述

Farmer John is distributing chocolates at the barn for Valentine’s day, and B (1 <= B <= 25,000) of his bulls have a special cow in mind to receive a chocolate gift.

Each of the bulls and cows is grazing alone in one of the farm’s N (2*B <= N <= 50,000) pastures conveniently numbered 1…N and connected by M (N-1 <= M <= 100,000) bidirectional cowpaths of various lengths. Some pastures might be directly connected by more than one cowpath. Cowpath i connects pastures R_i and S_i (1 <= R_i <= N; 1 <= S_i <= N) and has length L_i (1 <= L_i <= 2,000).

Bull i resides in pasture P_i (1 <= P_i <= N) and wishes to give a chocolate to the cow in pasture Q_i (1 <= Q_i <= N).

Help the bulls find the shortest path from their current pasture to the barn (which is located at pasture 1) and then onward to the pasture where their special cow is grazing. The barn connects, one way or another (potentially via other cowpaths and pastures) to every pasture.

As an example, consider a farm with 6 pastures, 6 paths, and 3 bulls (in pastures 2, 3, and 5) who wish to bestow chocolates on their love-objects:

*1  <-- Bull wants chocolates for pasture 1 cow[4]--3--[5]  <-- [5] is the pasture ID/  |/   |4    2          <-- 2 is the cowpath length/     |               between [3] and [4][1]--1--[3]*6/   \    /9     3  2/       \/[6]      [2]*4

* The Bull in pasture 2 can travel distance 3 (two different ways) to get to the barn then travel distance 2+1 to pastures [3] and [4] to gift his chocolate. That’s 6 altogether.

* The Bull in pasture 5 can travel to pasture 4 (distance 3), then pastures 3 and 1 (total: 3 + 2 + 1 = 6) to bestow his chocolate offer.

* The Bull in pasture 3 can travel distance 1 to pasture 1 and then take his chocolate 9 more to pasture 6, a total distance of 10.

输入格式

* Line 1: Three space separated integers: N, M, and B

* Lines 2…M+1: Line i+1 describes cowpath i with three

space-separated integers: R_i, S_i, and L_i

* Lines M+2…M+B+1: Line M+i+1 contains two space separated integers: P_i and Q_i

输出格式

* Lines 1…B: Line i should contain a single integer, the smallest distance that the bull in pasture P_i must travel to get chocolates from the barn and then award them to the cow of his dreams in pasture Q_i

样例 #1

样例输入 #1

6 7 3 
1 2 3 
5 4 3 
3 1 1 
6 1 9 
3 4 2 
1 4 4 
3 2 2 
2 4 
5 1 
3 6

样例输出 #1

6 
6 
10


题意解析

  • 找一个点先到 1 1 1 号点的最短距离,再找 1 1 1 号点到另一点的最短距离,求两者之和。
  • 乍一看以为是 F l o y d Floyd Floyd 算法,但是一看数据范围很大,那就并不是了,那还有什么算法能解决这种最短路问题呢?
  • 其实这并不是多源最短路问题,因为是双向图,所以你到我的最短路其实也是我到你的最短路,所以这题就变成了 1 1 1 号点到另外两个点的最短路之和的问题了,其实是单源最短路问题。
  • 考虑到 D i j k s t r a Dijkstra Dijkstra 可能超时,所以用 S P F A SPFA SPFA

CODE

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
#define ll long long
#define INF 0x3f3f3f3f using namespace std;typedef pair<int, int> pii;const int N = 50050, M = 2e5 + 10;
int h[N], e[M], ne[M], w[M], idx; // 定义图的存储结构
int dist[N]; // 存储每个节点到源点的最短距离
bool st[N]; // 存储每个节点是否在队列中
int n, m, b; // n是节点数,m是边的数目,b是查询的数目// 添加一条边
void add(int a, int b, int c){e[idx] = b; // 边的终点ne[idx] = h[a]; // 下一条相同起点的边w[idx] = c; // 边的权重h[a] = idx++; // 更新起点a的最后一条边
}// SPFA算法,用于求解单源最短路径
void spfa(){memset(dist, INF, sizeof dist); // 初始化所有节点到源点的距离为无穷大dist[1] = 0; // 源点到自己的距离为0queue<int> q;q.push(1); // 将源点加入队列st[1] = true; // 标记源点已经在队列中while(q.size()){auto t = q.front(); // 取出队首元素q.pop();st[t] = false; // 标记t已经不在队列中for(int i = h[t]; i != -1; i = ne[i]){ // 遍历所有从t出发的边int j = e[i];if(dist[j] > dist[t] + w[i]){ // 如果可以通过t到j的距离小于当前的最短距离dist[j] = dist[t] + w[i]; // 更新最短距离if(!st[j]){ // 如果j不在队列中q.push(j); // 将j加入队列st[j] = true; // 标记j已经在队列中}}}}
}int main()
{memset(h, -1, sizeof h); // 初始化邻接表cin >> n >> m >> b; // 输入节点数,边的数目,查询的数目while(m--){int a, b, c;scanf("%d%d%d", &a, &b, &c); // 输入边的信息add(a, b, c), add(b, a, c); // 将边添加到图中}spfa(); // 执行SPFA算法,求解最短路径while(b--){int p, q;scanf("%d%d", &p, &q); // 输入查询cout << dist[p] + dist[q] << endl; // 输出结果}    
}

给点思考

  • 为什么边权数组不用初始化为 I N F INF INF
    • 因为只有在遍历队列首元素t的所有出边时才会用到w[i],所以能用到肯定存在,所以不需要初始化。
  • 无向图该注意的问题:
    • 边数应该开两倍,因为无向,开少了就RE
http://www.hkea.cn/news/857444/

相关文章:

  • 网站的后台怎么做企业网络规划设计方案
  • 做网站文字字号大小企业网站设计要求
  • ae有么有做gif的网站品牌推广方案范文
  • apicloud官网下载seo关键词优化排名公司
  • 上海网站制作福州百度关键字优化精灵
  • 做uml图网站百度账号快速注册入口
  • 广西梧州南京 seo 价格
  • 网站警察备案seo关键词优化平台
  • 网站开发设计实训 报告惠州网站建设
  • 网站开发的原理山西免费网站关键词优化排名
  • 石家庄网站建设全包免费推广网站2024
  • 阿里云网站备案时间无锡seo网站管理
  • 景点介绍网站模板重庆百度关键词推广
  • 做亚马逊网站费用吗曲靖新闻今日头条
  • bing 网站管理员2023今日新闻头条
  • 深圳市做网站前十强百度一下搜索网页
  • 做执法设备有哪些网站国家免费培训学校
  • 顺德乐从有做阿里巴巴的网站吗杭州网站设计
  • 做英文网站 用阿里服务器行吗b2b网站推广排名
  • 搭建网站做淘宝客网赌怎么推广拉客户
  • 网站建设前台与后台最新技术2021最新免费的推广引流软件
  • 做网站基本语言淘宝如何提升关键词排名
  • wordpress怎样分类目录添加标签seo文章范文
  • 订阅号可以做网站吗南宁seo外包服务商
  • 邢台哪儿做网站便宜宁波 seo排名公司
  • 深圳网站优化咨询网上广告怎么推广
  • 网站右击无效是怎么做的网络营销产品
  • 中宣部网站政治建设网站服务器是什么意思
  • 淮安网站定制徐州seo外包公司
  • 嘉兴类网站系统总部网站建设技术解决方案