中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

直銷(xiāo)網(wǎng)寧波seo搜索平臺(tái)推廣專(zhuān)業(yè)

直銷(xiāo)網(wǎng),寧波seo搜索平臺(tái)推廣專(zhuān)業(yè),公眾號(hào)開(kāi)發(fā)周期,網(wǎng)站上傳圖片要求文章目錄 1137. 選擇最佳線路1131. 拯救大兵瑞恩1134. 最短路計(jì)數(shù)383. 觀光 dp是特殊的最短路&#xff0c;是無(wú)環(huán)圖&#xff08;拓?fù)鋱D&#xff09;上的最短路問(wèn)題 1137. 選擇最佳線路 1137. 選擇最佳線路 - AcWing題庫(kù) // 反向建圖就行 #include <iostream> #include…

文章目錄

      • 1137. 選擇最佳線路
      • 1131. 拯救大兵瑞恩
      • 1134. 最短路計(jì)數(shù)
      • 383. 觀光

dp是特殊的最短路,是無(wú)環(huán)圖(拓?fù)鋱D)上的最短路問(wèn)題

1137. 選擇最佳線路

1137. 選擇最佳線路 - AcWing題庫(kù)
image.png

// 反向建圖就行
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;typedef pair<int, int> PII;
const int N = 1e3 + 10, M = 2e4 + 10;
int h[N], e[M], ne[M], w[M], idx;
int n, m, s;
int a[N];
int dis[N]; bool st[N];void add(int x, int y, int d)
{e[idx] = y, ne[idx] = h[x], w[idx] = d, h[x] = idx ++ ;
}void dijkstra()
{priority_queue<PII, vector<PII>, greater<PII>> q;memset(dis, 0x3f, sizeof(dis));memset(st, 0, sizeof(st));dis[s] = 0;q.push({ dis[s], s });while (q.size()){auto t = q.top(); q.pop();int x = t.second, d = t.first;if (st[x]) continue;st[x] = true;for (int i = h[x]; i != -1; i = ne[i]){int y = e[i];if (dis[y] > d + w[i]) {dis[y] = d + w[i];q.push({ dis[y], y });}}}
}int main()
{while (~scanf("%d%d%d", &n, &m, &s)){idx = 0;memset(h, -1, sizeof(h));int x, y, d;while ( m -- ){scanf("%d%d%d", &x, &y, &d);add(y, x, d);}int wn;scanf("%d", &wn);for (int i = 1; i <= wn; ++ i ) scanf("%d", &a[i]);dijkstra();int res = 0x3f3f3f3f;for (int i = 1; i <= wn; ++ i ) res = min(res, dis[a[i]]);if (res == 0x3f3f3f3f) puts("-1");else printf("%d\n", res);}return 0;
}

對(duì)于每組測(cè)試數(shù)據(jù),該重置的數(shù)據(jù)要重置,我沒(méi)有重置idx,導(dǎo)致TLE

處理反向建圖,還有一種擴(kuò)展做法:虛擬源點(diǎn)

設(shè)置虛擬源點(diǎn),與每個(gè)起點(diǎn)之間連接邊權(quán)為0的邊
原問(wèn)題:從多個(gè)源點(diǎn)出發(fā),到達(dá)終點(diǎn)的最短路徑
先問(wèn)題:從虛擬源點(diǎn)出發(fā),到達(dá)終點(diǎn)的最短路徑
兩者的最短路徑一一對(duì)應(yīng),并且路徑和相同
image.png

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;typedef pair<int, int> PII;
const int N = 1e3 + 10, M = 3e4 + 10;
int h[N], e[M], ne[M], w[M], idx;
int n, m, s;
int a[N];
int dis[N]; bool st[N];void add(int x, int y, int d)
{e[idx] = y, ne[idx] = h[x], w[idx] = d, h[x] = idx ++ ;
}void dijkstra()
{priority_queue<PII, vector<PII>, greater<PII>> q;memset(dis, 0x3f, sizeof(dis));memset(st, 0, sizeof(st));dis[0] = 0;q.push({ dis[0], 0 });while (q.size()){auto t = q.top(); q.pop();int x = t.second, d = t.first;if (st[x]) continue;st[x] = true;for (int i = h[x]; i != -1; i = ne[i]){int y = e[i];if (dis[y] > d + w[i]) {dis[y] = d + w[i];q.push({ dis[y], y });}}}
}int main()
{while (~scanf("%d%d%d", &n, &m, &s)){idx = 0;memset(h, -1, sizeof(h));int x, y, d;while ( m -- ){scanf("%d%d%d", &x, &y, &d);add(x, y, d);}int wn;scanf("%d", &wn);for (int i = 1; i <= wn; ++ i ) {scanf("%d", &a[i]);add(0, a[i], 0);  // 設(shè)置虛擬源點(diǎn)}dijkstra();if (dis[s] == 0x3f3f3f3f) puts("-1");else printf("%d\n", dis[s]);}return 0;
}

debug:將虛擬源點(diǎn)與起點(diǎn)之間建立邊,要注意M的大小是否足夠,又是M開(kāi)小了…


1131. 拯救大兵瑞恩

1131. 拯救大兵瑞恩 - AcWing題庫(kù)
image.png

從集合的角度分析
狀態(tài)表示:
集合:起點(diǎn)為左上角,終點(diǎn)為圖中任意一點(diǎn)的所有路徑,用 f ( x , y ) f(x, y) f(x,y)表示終點(diǎn)為 [ x , y ] [x, y] [x,y]的路徑
屬性:最小時(shí)間(路徑和)
所以 f ( x , y ) f(x, y) f(x,y)表示終點(diǎn)為 [ x , y ] [x, y] [x,y]的最小路徑和
但是圖中存在無(wú)法通過(guò)的墻以及需要鑰匙打開(kāi)的門(mén),所以用兩個(gè)維度表示路徑將無(wú)法更新集合
考慮增加一個(gè)維度 s t a t e state state,狀態(tài)壓縮,表示擁有的鑰匙狀態(tài)
f ( x , y , s t a t e ) f(x, y, state) f(x,y,state)表示擁有鑰匙的狀態(tài)為 s t a t e state state時(shí),遞達(dá) [ x , y ] [x, y] [x,y]的最短路

狀態(tài)計(jì)算:
如何劃分 f ( x , y , s t a t e ) f(x, y, state) f(x,y,state)?一般的dp問(wèn)題是從后往前考慮,圖論中的集合分析一般從前往后考慮
f ( x , y , s t a t e ) f(x, y, state) f(x,y,state)能推導(dǎo)出哪些集合?
[ x , y ] [x, y] [x,y]有鑰匙,可以撿起這些鑰匙,假設(shè)鑰匙的狀態(tài)為key,那么狀態(tài)推導(dǎo)就是 f ( x , y , s t a t e ) ? > f ( x , y , s t a t e ∣ k e y ) f(x, y, state)->f(x, y, state | key) f(x,y,state)?>f(x,y,statekey)
[ x , y ] [x, y] [x,y]無(wú)鑰匙,那么可以向相鄰的位置走, f ( x , y , s t a t e ) ? > f ( n x , n y , s t a t e ) f(x, y, state)->f(nx, ny, state) f(x,y,state)?>f(nx,ny,state),此時(shí)的最短距離要+1
由于這個(gè)問(wèn)題中存在環(huán)路,所以無(wú)法用dp更新集合,只能用最短路算法更新集合

這題比較麻煩的是:建邊,相鄰兩個(gè)位置若沒(méi)有墻,那么可以建立一條權(quán)值為1的邊
如何表示兩個(gè)二維坐標(biāo)之間有邊?這里涉及到二維坐標(biāo)到一維的轉(zhuǎn)換,然后用鄰接表存儲(chǔ)圖
若兩個(gè)位置之間存在門(mén),用邊權(quán)表示門(mén)的種類(lèi),但是實(shí)際的邊權(quán)為1
若兩個(gè)位置之間既不存在門(mén),也不存在墻,那么創(chuàng)建一條權(quán)值為0的邊,但時(shí)間的邊權(quán)為1。所以 w [ i ] w[i] w[i]為非0表示這個(gè)邊上有道門(mén),為0表示可以直接通過(guò)
對(duì)于墻的情況,直接忽略,不建立邊(表示不連通)即可
用set存儲(chǔ)已經(jīng)建立的邊,防止重復(fù)建邊

#include <iostream>
#include <cstring>
#include <deque>
#include <set>
using namespace std;typedef pair<int, int> PII;
const int N = 11, P = 1 << N;
const int M = 400;
int h[N * N], e[M], ne[M], w[M], idx;
int g[N][N]; // 二維到一維的轉(zhuǎn)換
int key[N * N]; // 每個(gè)坐標(biāo)的鑰匙狀態(tài)
int dis[N * N][P]; bool st[N * N][P];
set<PII> s;int n, m, p, k;void add(int x, int y, int d)
{e[idx] = y, ne[idx] = h[x], w[idx] = d, h[x] = idx ++ ;
}void build()
{int dx[4] = { 0, 1, 0, -1}, dy[4] = { 1, 0, -1, 0 };for (int x = 1; x <= n; ++ x )for (int y = 1; y <= m; ++ y )for (int i = 0; i < 4; ++ i ){int nx = x + dx[i], ny = y + dy[i];if (nx > 0 && nx <= n && ny > 0 && ny <= m){int a = g[x][y], b = g[nx][ny];if (!s.count({a, b})) add(a, b, 0);}}
}int bfs()
{memset(dis, 0x3f, sizeof(dis));deque<PII> q;dis[1][0] = 0;q.push_back({1, 0});while (q.size()){auto t = q.front(); q.pop_front();int x = t.first, state = t.second;if (st[x][state]) continue;st[x][state] = true;if (x == n * m) return dis[n * m][state];if (key[x]){int nstate = state | key[x];if (dis[x][nstate] > dis[x][state]){dis[x][nstate] = dis[x][state];q.push_front({x, nstate});}}for (int i = h[x]; i != -1; i = ne[i]){int y = e[i];if (w[i] && !((state >> w[i]) & 1)) continue;if (dis[y][state] > dis[x][state] + 1){dis[y][state] = dis[x][state] + 1;q.push_back({y, state});}}}   return -1;
}int main()
{memset(h, -1, sizeof(h));scanf("%d%d%d%d", &n, &m, &p, &k);int cnt = 1;for (int i = 1; i <= n; ++ i )for (int j = 1; j <= m; ++ j ) g[i][j] = cnt ++ ;int x1, y1, x2, y2, x, y, d;while ( k -- ){scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &d);x = g[x1][y1], y = g[x2][y2];s.insert({x, y}), s.insert({y, x});if (d) add(x, y, d), add(y, x, d);}build(); // 建立除了門(mén)和墻的邊int l;scanf("%d", &l);while ( l -- ){scanf("%d%d%d", &x, &y, &d);key[g[x][y]] |= 1 << d;}printf("%d\n", bfs());return 0;
}

debug:int x = t.first, state = t.second寫(xiě)成int x = t.secnd, state = t.first
只能說(shuō)是dijkstra寫(xiě)多了


1134. 最短路計(jì)數(shù)

1134. 最短路計(jì)數(shù) - AcWing題庫(kù)
image.png

image.png

從集合的角度考慮, f ( i ) f(i) f(i)表示圖中第i個(gè)點(diǎn)的最短路條數(shù),假設(shè)與i相連的點(diǎn)由k個(gè),那么 f ( i ) = f ( s 1 ) + f ( s 2 ) + . . . + f ( s k ) f(i) = f(s_1) + f(s_2) + ... + f(s_k) f(i)=f(s1?)+f(s2?)+...+f(sk?),第i個(gè)點(diǎn)的最短路條數(shù)由與之直接相連的點(diǎn)的最短路條數(shù)累加而成
那么要求解 f ( i ) f(i) f(i),就要先算出它的子集,但是圖論問(wèn)題可能存在環(huán),無(wú)法確定 f ( i ) f(i) f(i)是否會(huì)影響它的子集。所以只能在拓?fù)鋱D中才能這樣更新集合,考慮最短路算法的更新是否具有拓?fù)湫?/p>

三種求最短路的方法:1.BFS 2.Dijkstra 3.Bellman-ford
探討它們求解最短路時(shí),是否具有拓?fù)湫?#xff1f;
對(duì)于BFS,由于每個(gè)點(diǎn)只會(huì)入隊(duì)一次且只會(huì)出隊(duì)一次,說(shuō)明BFS的更新天然地具有拓?fù)湫?#xff0c;因?yàn)槌鲫?duì)的點(diǎn)不會(huì)被后續(xù)入隊(duì)的點(diǎn)影響
對(duì)于Dijkstra,由于每個(gè)點(diǎn)會(huì)入隊(duì)多次,但只會(huì)出隊(duì)一次,也說(shuō)明了Dijkstra的更新天然地具有拓?fù)湫?br /> 對(duì)于spfa,由于它是暴力算法的優(yōu)化,每個(gè)點(diǎn)都會(huì)入隊(duì)與出隊(duì)多次,所以spfa的更新不具有拓?fù)湫?#xff0c;已經(jīng)出隊(duì)(更新完成)的點(diǎn)可能影響被后續(xù)入隊(duì)的點(diǎn)影響
即bfs和dijkstra的更新是一顆最短路樹(shù),而spfa的更新不是一顆最短路樹(shù)
image.png

統(tǒng)計(jì)最短路條數(shù)時(shí),可以遍歷最短路樹(shù)
若統(tǒng)計(jì)i節(jié)點(diǎn)的最短路條數(shù),只需要累乘父節(jié)點(diǎn)的數(shù)量即可
而spfa的更新不具有拓?fù)湫?#xff0c;即不存在最短路樹(shù),要是圖中存在負(fù)權(quán)邊,無(wú)法使用天然具有拓?fù)湫虻腷fs和dijkstra時(shí),只能先用spfa求出最短路,維護(hù)出最短路樹(shù),再求最短路條數(shù)

一般情況下,圖中不能存在權(quán)值為0的點(diǎn),否則無(wú)法建立出最短路樹(shù),因?yàn)檫_(dá)到某一個(gè)點(diǎn)的最短路不能確定

這題直接用bfs更新最短路,在更新過(guò)程中完成最短路條數(shù)的統(tǒng)計(jì):用x更新y時(shí),dis[y] > dis[x] + 1時(shí),y的最短路數(shù)量等于x的最短路數(shù)量
dis[y] == dix[x] + 1,y的最短路條數(shù)等于兩者的數(shù)量累加

#include <iostream>
#include <cstring>
using namespace std;const int N = 1e5 + 10, M = 4e5 + 10, mod = 100003;
int h[N], e[M], ne[M], idx;
int dis[N], q[N], hh, tt = -1;
int cnt[N];
int n, m;void add(int x, int y)
{e[idx] = y, ne[idx] = h[x], h[x] = idx ++ ;
}void bfs()
{memset(dis, 0x3f, sizeof(dis));q[++ tt ] = 1;dis[1] = 0, cnt[1] = 1;while (tt >= hh){int x = q[hh ++ ];for (int i = h[x]; i != -1; i = ne[i]){int y = e[i];if (dis[y] > dis[x] + 1){dis[y] = dis[x] + 1;q[++ tt ] = y;cnt[y] = cnt[x];}else if(dis[y] == dis[x] + 1) cnt[y] = (cnt[y] + cnt[x]) % mod;}}
}int main()
{memset(h, -1, sizeof(h));scanf("%d%d", &n, &m);int x, y;while ( m -- ){scanf("%d%d", &x, &y);add(x, y), add(y, x);}bfs();for (int i = 1; i <= n; ++ i ) {if (cnt[i] == 0x3f3f3f3f) puts("0");else printf("%d\n", cnt[i]);}return 0;
}

383. 觀光

383. 觀光 - AcWing題庫(kù)
image.png

由于無(wú)負(fù)權(quán)邊,所以用dijkstra更新最短路,同時(shí)維護(hù)最短路條數(shù)
但是題目還要維護(hù)最短路條數(shù),所以這里用了個(gè)類(lèi)似拯救大兵瑞恩的思想:狀壓
dis[i][0]表最短路距離,dis[i][1]表示次短路距離,由于次短路的更新也具有拓?fù)湫?#xff0c;所以我們可以在更新次短路的時(shí)候維護(hù)次短路條數(shù)

d i s [ i ] [ 1 ] dis[i][1] dis[i][1]如何計(jì)算?與i相連的所有點(diǎn)的最短路以及次短路中,第二大的數(shù)
代碼體現(xiàn)在:
dis[y][0] > dis[x][0] + w[i],則更新最短路 d i s [ y ] [ 0 ] dis[y][0] dis[y][0],那么最短路成為次短路 d i s [ y ] [ 1 ] dis[y][1] dis[y][1],更新次短路,同時(shí)更新最短路
dis[y][0] == dis[x][0] + w[i],那么最短路條數(shù)累加,cnt[y][0] += cnt[x][0]
dis[y][1] > dis[x][0] + w[i],那么更新次短路 d i s [ y ] [ 1 ] dis[y][1] dis[y][1]
dis[y][1] == dis[x][0] + w[i],那么次短路條數(shù)累加,cnt[y][1] += cnt[x][1]

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;const int N = 1010, M = 10010;
int h[N], e[M], ne[M], w[M], idx;
int n, m, s, t;
int dis[N][2], cnt[N][2]; bool st[N][2];struct Ver
{int x, d, type;bool operator>(const Ver& v) const // 建小堆重載>{return d > v.d;}
};void add(int x, int y, int d)
{e[idx] = y, ne[idx] = h[x], w[idx] = d, h[x] = idx ++ ;
}int dijkstra()
{memset(dis, 0x3f, sizeof(dis));memset(st, 0, sizeof(st));memset(cnt, 0, sizeof(cnt));priority_queue<Ver, vector<Ver>, greater<Ver>> q;q.push({s, 0, 0});dis[s][0] = 0, cnt[s][0] = 1;while (q.size()){auto t = q.top(); q.pop();int x = t.x, d = t.d, type = t.type;int count = cnt[x][type];if (st[x][type]) continue;st[x][type] = true;for (int i = h[x]; i != -1; i = ne[i]){int y = e[i];if (dis[y][0] > d + w[i]){dis[y][1] = dis[y][0], cnt[y][1] = cnt[y][0];q.push({y, dis[y][1], 1});dis[y][0] = d + w[i], cnt[y][0] = count;q.push({y, dis[y][0], 0});}else if (dis[y][0] == d + w[i]) cnt[y][0] += count;else if(dis[y][1] > d + w[i]){dis[y][1] = d + w[i], cnt[y][1] = count;q.push({y, dis[y][1], 1});}else if (dis[y][1] == d + w[i]) cnt[y][1] += count;}}int res = cnt[t][0];if (dis[t][0] + 1== dis[t][1]) res += cnt[t][1];return res;
}int main()
{int T;scanf("%d", &T);while ( T -- ){idx = 0;memset(h, -1, sizeof(h));scanf("%d%d", &n, &m);int x, y, d;while ( m -- ){scanf("%d%d%d", &x, &y, &d);add(x, y, d);}scanf("%d%d", &s, &t);printf("%d\n", dijkstra());}return 0;
}
http://www.risenshineclean.com/news/62974.html

相關(guān)文章:

  • 西安抖音代運(yùn)營(yíng)公司seo優(yōu)化是什么
  • wordpress經(jīng)典漏洞搜外seo
  • 濰坊程序設(shè)計(jì)網(wǎng)站建設(shè)公司代運(yùn)營(yíng)哪家比較可靠
  • 做網(wǎng)站系統(tǒng)用什么語(yǔ)言鄭州seo教程
  • 如何制作旅游網(wǎng)站鄭州關(guān)鍵詞優(yōu)化顧問(wèn)
  • 一個(gè)網(wǎng)站開(kāi)發(fā)流程圖永久免費(fèi)跨境瀏覽app
  • 科技有限公司可以做網(wǎng)站建設(shè)嗎?廣州網(wǎng)絡(luò)推廣公司有哪些
  • 匯算清繳在哪個(gè)網(wǎng)站做百度貼吧廣告投放
  • 網(wǎng)站建設(shè)的三網(wǎng)合一廣州全網(wǎng)推廣
  • 網(wǎng)站域名登記證明在線搭建網(wǎng)站
  • 學(xué)校ftp服務(wù)器做網(wǎng)站泰安seo
  • 做網(wǎng)站賣(mài)廣告位賺錢(qián)嗎百度站長(zhǎng)工具平臺(tái)登錄
  • 做網(wǎng)站是要編程嗎網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣總結(jié)
  • 南通網(wǎng)站外包2021年搜索引擎排名
  • 建設(shè)部城市管理監(jiān)督局網(wǎng)站官網(wǎng)南京百度seo排名
  • 網(wǎng)站開(kāi)發(fā)后臺(tái)注意事項(xiàng)咨詢(xún)公司
  • 手機(jī)app微信網(wǎng)站建設(shè)磁力狗在線搜索
  • 自己做的網(wǎng)站如何上傳百度電話銷(xiāo)售
  • 表白網(wǎng)站制作平臺(tái)百度信息流投放
  • 垃圾網(wǎng)站信息怎么辦朋友圈營(yíng)銷(xiāo)廣告
  • 東莞網(wǎng)站優(yōu)化怎樣百度最新秒收錄方法2021
  • 番禺做網(wǎng)站企業(yè)1688seo優(yōu)化是什么
  • 自己做網(wǎng)站空間百度搜索廣告
  • wordpress自適應(yīng)導(dǎo)航模板seo的基本步驟
  • 單位網(wǎng)里建網(wǎng)站培訓(xùn)心得體會(huì)總結(jié)
  • 微信營(yíng)銷(xiāo) 網(wǎng)站建設(shè)關(guān)鍵詞排名手機(jī)優(yōu)化軟件
  • u盤(pán)搭建網(wǎng)站開(kāi)發(fā)環(huán)境方法線上推廣方式都有哪些
  • 騰云建站靠譜嗎seo是指什么崗位
  • 合肥網(wǎng)站制作軟件湖南網(wǎng)站建設(shè)效果
  • 國(guó)外優(yōu)秀的平面設(shè)計(jì)網(wǎng)站seo系統(tǒng)培訓(xùn)