Luogu3350 ZJOI2016 旅行者 最短路、分治

傳送門

題意:給出一個$N \times M$的網格圖,邊有邊權,$Q$組詢問,每組詢問$(x_1,y_1)$到$(x_2,y_2)$的最短路。$N \times M \leq 2 \times 10^4 , Q \leq 10^5$


BZOJ原題竟然沒有數據范圍

矩形的多組詢問問題考慮分治。考慮計算矩形$(x_1,y_1,x_2,y_2)$的詢問,我們將較長邊沿著中線劈成兩半,在這些詢問里面就只可能存在兩種情況:①詢問的兩個端點在中線兩邊,那么路徑就一定會經過中線上一點;②詢問的兩個端點在中線的同一邊,那么有可能不會經過中線,也有可能經過中線。所以我們對中線上所有的點跑一邊整個矩形的最短路,每一次跑完最短路就更新所有的詢問,然后再遞歸進入兩側矩形回答②類型的詢問。時間復雜度似乎是$O(NM \times \sqrt{NM} \times log(NM))$

在$Luogu$題解上學到的一個加速方法:從一個點的最短路轉移到新的點跑最短路的時候,可以不將最短路數組賦值為極大值,而是賦值為這一個點經過上一個計算的點到達所有目標點的答案。

然而還是在Luogu不開O2的情況下TLE

  1 // luogu-judger-enable-o2
  2 //This code is written by Itst
  3 #include<bits/stdc++.h>
  4 #define pos(i,j) ((i-1)*M+j)
  5 using namespace std;
  6 
  7 inline int read(){
  8     int a = 0;
  9     bool f = 0;
 10     char c = getchar();
 11     while(c != EOF && !isdigit(c)){
 12         if(c == '-')
 13             f = 1;
 14         c = getchar();
 15     }
 16     while(c != EOF && isdigit(c)){
 17         a = (a << 3) + (a << 1) + (c ^ '0');
 18         c = getchar();
 19     }
 20     return f ? -a : a;
 21 }
 22 
 23 const int MAXN = 100010;
 24 int cntEd , N , M , Q , minDis[MAXN] , ans[MAXN] , head[MAXN];
 25 struct Edge{
 26     int end , upEd , w;
 27 }Ed[MAXN << 2];
 28 struct query{
 29     int sx , sy , ex , ey , ind;
 30 }now[MAXN] , pot[MAXN];
 31 priority_queue < pair < int , int > > q;
 32 
 33 inline void addEd(int a , int b , int c){
 34     Ed[++cntEd].end = b;
 35     Ed[cntEd].w = c;
 36     Ed[cntEd].upEd = head[a];
 37     head[a] = cntEd;
 38 }
 39 
 40 void Dijk(int bx , int by , int lx , int ly , int rx , int ry , int l){
 41     int temp = minDis[pos(bx , by)];
 42     for(int i = lx ; i <= rx ; i++)
 43         for(int j = ly ; j <= ry ; j++)
 44             minDis[pos(i , j)] = l == 0 ? 0x3f3f3f3f : minDis[pos(i , j)] + temp;
 45     minDis[pos(bx , by)] = 0;
 46     q.push(make_pair(0 , pos(bx , by)));
 47     while(!q.empty()){
 48         pair < int , int > t = q.top();
 49         q.pop();
 50         if(minDis[t.second] != -t.first)
 51             continue;
 52         for(int i = head[t.second] ; i ; i = Ed[i].upEd)
 53             if(minDis[Ed[i].end] > minDis[t.second] + Ed[i].w){
 54                 minDis[Ed[i].end] = minDis[t.second] + Ed[i].w;
 55                 q.push(make_pair(-minDis[Ed[i].end] , Ed[i].end));
 56             }
 57     }
 58 }
 59 
 60 void solve(int lx , int ly , int rx , int ry , int ql , int qr){
 61     if(ql > qr)
 62         return;
 63     if(rx - lx > ry - ly){
 64         int mid = lx + rx >> 1;
 65         for(int i = ly ; i <= ry ; i++){
 66             Dijk(mid , i , lx , ly , rx , ry , i - ly);
 67             for(int j = ql ; j <= qr ; j++)
 68                 ans[now[j].ind] = min(ans[now[j].ind] , minDis[pos(now[j].sx , now[j].sy)] + minDis[pos(now[j].ex , now[j].ey)]);
 69         }
 70         int p1 = ql , p2 = qr;
 71         for(int i = ql ; i <= qr ; i++)
 72             if(now[i].sx < mid && now[i].ex < mid)
 73                 pot[p1++] = now[i];
 74             else
 75                 if(now[i].sx > mid && now[i].ex > mid)
 76                     pot[p2--] = now[i];
 77         memcpy(now + ql , pot + ql , sizeof(query) * (qr - ql + 1));
 78         solve(lx , ly , mid , ry , ql , p1 - 1);
 79         solve(mid + 1 , ly , rx , ry , p2 + 1 , qr);
 80     }
 81     else{
 82         int mid = ly + ry >> 1;
 83         for(int i = lx ; i <= rx ; i++){
 84             Dijk(i , mid , lx , ly , rx , ry , i - lx);
 85             for(int j = ql ; j <= qr ; j++)
 86                 ans[now[j].ind] = min(ans[now[j].ind] , minDis[pos(now[j].sx , now[j].sy)] + minDis[pos(now[j].ex , now[j].ey)]);
 87         }
 88         int p1 = ql , p2 = qr;
 89         for(int i = ql ; i <= qr ; i++)
 90             if(now[i].sy < mid && now[i].ey < mid)
 91                 pot[p1++] = now[i];
 92             else
 93                 if(now[i].sy > mid && now[i].ey > mid)
 94                     pot[p2--] = now[i];
 95         memcpy(now + ql , pot + ql , sizeof(query) * (qr - ql + 1));
 96         solve(lx , ly , rx , mid , ql , p1 - 1);
 97         solve(lx , mid + 1 , rx , ry , p2 + 1 , qr);
 98     }
 99 }
100 
101 int main(){
102 #ifdef LG
103     freopen("3350.in" , "r" , stdin);
104     freopen("3350.out" , "w" , stdout);
105 #endif
106     memset(ans , 0x3f , sizeof(ans));
107     N = read();
108     M = read();
109     for(int i = 1 ; i <= N ; i++)
110         for(int j = 1 ; j < M ; j++){
111             int t = read();
112             addEd(pos(i , j) , pos(i , j + 1) , t);
113             addEd(pos(i , j + 1) , pos(i , j) , t);
114         }
115     for(int i = 1 ; i < N ; i++)
116         for(int j = 1 ; j <= M ; j++){
117             int t = read();
118             addEd(pos(i , j) , pos(i + 1 , j) , t);
119             addEd(pos(i + 1 , j) , pos(i , j) , t);
120         }
121     Q = read();
122     for(int i = 1 ; i <= Q ; i++){
123         now[i].sx = read();
124         now[i].sy = read();
125         now[i].ex = read();
126         now[i].ey = read();
127         now[i].ind = i;
128     }
129     solve(1 , 1 , N , M , 1 , Q);
130     for(int i = 1 ; i <= Q ; i++)
131         printf("%d\n" , ans[i]);
132     return 0;
133 }

轉載于:https://www.cnblogs.com/Itst/p/9879787.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/249112.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/249112.shtml
英文地址,請注明出處:http://en.pswp.cn/news/249112.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Mac中安裝Node和版本控制工具nvm遇到的坑

首先說說常規的安裝 1. 下載nvm,使用nvm來管理Node版本 官方文檔 windows 版本  https://github.com/coreybutler/nvm-windows mac 版本    https://github.com/creationix/nvm#install-script 命令行 盡量不要用brew&#xff0c;免得掉坑 curl -o- https://raw.githubu…

幾道比較有意思的js面試題

1、[] ? !![] : ![];輸出結果是什么&#xff1f; 12345let val [] ? !![] : ![];console.log(val); //true&#xff1a;//之前的錯誤解釋&#xff1a;[] 是一個null&#xff0c;做判斷則為false&#xff0c;false執行![]語句&#xff0c;結果為非空&#xff0c;即true//更正…

wepy - 與原生有什么不同(x.wpy)使用實例

源碼 1 <template>2 <view classmark wx:if"{{showMark}}">3 <view animation"{{animationData}}" class"animCat">4 <image src"http://osk1hpe2y.bkt.clouddn.com/18-5-30/34559443.jpg"></…

vue從入門到精通之高級篇(一)vue-router的高級用法

今天要介紹的是路由元信息&#xff0c;滾動行為以及路由懶加載這幾個的使用方法。 1.路由元信息 什么是路由元信息&#xff0c;看看官網的解釋&#xff0c;定義路由的時候可以配置 meta 字段可以匹配meta字段&#xff0c;那么我們該如何使用它&#xff0c;一個簡單的例子&…

Java 數組實現堆棧操作

class Stack {private int stck[] ; private int tos ; Stack(int size) { // 一個參數的構造參數stck new int[size] ; // 創建數組&#xff08;創建堆棧&#xff09;tos -1 ; // 空堆棧標識 -1}// 堆棧操作的特性&#xff1a;先進后出、后進先出void push(int…

re模塊

什么是正則表達式 一組特殊符號組成的表達式&#xff0c;用于描述某種規則。該應用場景生活中隨處可見。 例如&#xff1a;讓有志青年過上體面的生活&#xff0c;這里面就由規則&#xff0c;即有志青年。 正則表達式的作用&#xff0c;以及使用場景 用于從字符串中匹配滿足某種…

CSS實現div梯形分割

原理 使用的border邊框屬性結合svg 轉換 詳見代碼 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>css實現div邊框斜角</title><style type"text/css"> .labels {display: i…

算法學習——決策單調性優化DP

update in 2019.1.21 優化了一下文中年代久遠的代碼 的格式…… 什么是決策單調性&#xff1f; 在滿足決策單調性的情況下&#xff0c;通常決策點會形如1111112222224444445555588888..... 即不可能會出現后面點的決策點小于前面點的決策點這種情況。 那么這個性質應該如何使用…

SVG畫一個箭頭

參考菜鳥手冊&#xff1a; https://www.runoob.com/svg/svg-tutorial.html 打開菜鳥中的在線工具 在可視化截圖拖拉元素繪制箭頭 點擊command U 查看源碼 將源碼拷入html代碼中&#xff0c;查看效果 最后&#xff0c;貼出源碼供大家參考 <!DOCTYPE html> <…

HP Instant Information

HP Instant Information before HP-UX 11i v3 《管理系統和工作組&#xff1a;HP-UX系統管理員指南》 After HP-UX 11i v3 《HP-UX系統管理指南》(由多個文檔組成的文檔集) 《HP-UX系統管理員指南&#xff1a;概述》 《HP-UX系統管理員指南&#xff1a;配置管理》 《HP-UX系統管…

CodeForces 258D Little Elephant and Broken Sorting(期望)

CF258D Little Elephant and Broken Sorting 題意 題意翻譯 有一個\(1\sim n\)的排列&#xff0c;會進行\(m\)次操作&#xff0c;操作為交換\(a,b\)。每次操作都有\(50\%\)的概率進行。 求進行\(m\)次操作以后的期望逆序對個數。 \(n,m\le 1000\) 輸入輸出格式 輸入格式&#x…

記一次vue項目yarn打包環境配置失效的解決方案

項目中使用到了yarn打包工程&#xff0c;主要有以下幾個命名。 # build for production with minification yarn run build# build for production and view the bundle analyzer report yarn run build --report# 自定義API地址 baseurl"http://127.0.0.1:8080/api/&quo…

數字簽名與HTTPS詳解

因為HTTP協議本身存在著明文傳輸、不能很好的驗證通信方的身份和無法驗證報文的完整性等一些安全方面的確點&#xff0c;所以才有了HTTPS的缺陷。HTTPS確切的的說不是一種協議&#xff0c;而是HTTP SSL (TSL)的結合體。HTTP報文經過SSL層加密后交付給TCP層進行傳輸。SSL(安全套…

[BZOJ4320][ShangHai2006]Homework(根號分治+并查集)

對于<sqrt(300000)的詢問&#xff0c;對每個模數直接記錄結果&#xff0c;每次加入新數時暴力更新每個模數的結果。 對于>sqrt(300000)的詢問&#xff0c;枚舉倍數&#xff0c;每次查詢大于等于這個倍數的最小數是多少&#xff0c;這個操作通過將詢問逆序使用并查集支持。…

VScode 結局插件prettier和vetur格式化沖突

先上配置代碼 {"workbench.iconTheme": "vscode-icons","workbench.startupEditor": "newUntitledFile","workbench.colorTheme": "One Dark Pro","editor.fontSize": 14,"editor.tabSize":…

WPF效果(GIS三維續篇)

去年這個時候簡單的摸索了一下三維的GIS相關的東西,也就是僅僅玩耍了一把,這次來點真正用的上的干貨效果效果&#xff1a; 1、加載自定義百度樣式的瓦片效果 2、加載自定義百度樣式的縮放效果 3、快速手動進去咱的大帝都 4、加載海量Mark效果 5、加載海量Mark和簡單模型效果 6、…

vue 表單 驗證 async-validator

1、使用插件async-validator async-validator 地址&#xff1a;https://github.com/yiminghe/async-validator 2、示例&#xff08;vueelement-ui&#xff09; <el-form :model"numberValidateForm" ref"numberValidateForm" label-width"100px&qu…

[19/04/23-星期二] GOF23_創建型模式(工廠模式、抽象工廠模式)

一、工廠模式(分為&#xff1a;簡單工廠模式、工廠方法模式、抽象工廠模式) 實現了創建者和調用者的分離 核心本質&#xff1a;1、實例化對象&#xff0c;用工廠方法代替new操作&#xff1b;2、將選擇實現類、創建對象統一管理和控制&#xff0c;從而將調用者跟實現類解耦。 簡…

Chrome瀏覽器12px問題-webkit-text-size-adjust: none 已失效的解決方案

對于早期的chrome, 如果要想顯示12px以下的字體&#xff0c;一般通用的方案都是在對應的元素中添加 div {-webkit-text-size-adjust: none; }但是我今天遇到的需求&#xff0c;添加了之后沒有反應&#xff0c;而且瀏覽就根本不支持這種寫法。 在網上看到了博客《Chrome瀏覽器…

CSRFGuard工具介紹

理解CSRFGuard的基礎&#xff1a;http://www.runoob.com/jsp/jsp-tutorial.html 1&#xff1a;您需要做的第一件事是將OWASP.CSRFARGAD.JAR庫復制到類路徑中。放置Owasp.CsrfGuard.jar最常見的類路徑位置在Web應用程序的WEB-INF文件夾的lib目錄中。 OWASP CSRFGARD 3在傳統Java…