發表文章

目前顯示的是有「dijkstra」標籤的文章

[AtCoder] [經典競程 90 題] 013 - Passing(★5)

題目連結: https://atcoder.jp/contests/typical90/tasks/typical90_m 題目大意: 給一個 $N$ 個點 $M$ 條邊的圖,對於所有點 $u$ 求問強迫經過點 $u$ 時 $1$ 走到 $n$ 的最短路是多長。 從 $1$ 跟 $N$ 分別做一次 Dijkstra 就好了。 #include <bits/stdc++.h> using namespace std; using lld = int64_t; const lld INF = static_cast<lld>(1) << 60; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<pair<int, lld>>> g(n); for (int i = 0; i < m; ++i) { int u, v, w; cin >> u >> v >> w; g[--u].emplace_back(--v, w); g[v].emplace_back(u, w); } auto shortest_path = [&g] (int s, vector<lld> &d) { vector<bool> vis(d.size()); fill(d.begin(), d.end(), INF); priority_queue<pair<lld,int>> pq; d[s] = 0; pq.emplace(-d[s], s); while (n...

[TIOJ] 1847. 在男子高校尋求邂逅是否搞錯了什麼?

題目連結: http://tioj.infor.org/problems/1847 題目看了一下下才看懂,原來就是給你一張圖問你最短距離小於D的點權和。那要求最短路徑就寫個dijkstra就好,然後再掃一下看某個點距離是不是小於D,是就加點權,不是就不加。 #include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; #define FF first #define SS second #define PB push_back const int N = 100000 + 5; const int INF = (1<<30) + 5; vector<int> G[N]; int dis[N], val[N]; bitset<N> visited; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n, m, d; cin>>n>>m; for(int i=0;i<n;i++) cin>>val[i]; for(int i=0;i<m;i++){ int u, v; cin>>u>>v; G[u].PB(v); G[v].PB(u); } cin>>d; fill(dis, dis+n, INF); priority_queue<PII,vector<PII>,greater<PII>> pq; dis[0]=0; pq.push({dis[0], 0}); while(!pq.empty()){ int u = pq.top().SS; pq.pop(); ...

[TIOJ] 1641. 貨物運送計劃

題目連結: http://tioj.infor.org/problems/1641 看完 CBD的題解 後,覺得自己滿蠢的,其實取個log後最dijkstra就好了www。我這裡是自己寫了個簡單(?的科學記號模板來用,做法就依樣是裸的dijkstra。 #include <bits/stdc++.h> using namespace std; #define PB push_back typedef pair<double,int> PDI; #define FF first #define SS second const int N = 10000 + 5; struct SciFi{ double x; int p; SciFi(){x=0;p=0;} SciFi(double k){ p = floor(log10(k)); x = k / pow((double)10, p); } SciFi(double a, int b){ x=a;p=b; } SciFi operator=(double k){ p = floor(log10(k)); x = k / pow((double)10, p); return *this; } SciFi operator*(SciFi k)const{ int nP = p+k.p; double nX = x*k.x; int tp = floor(log10(nX)); return SciFi(nX/pow((double)10, tp), ...

[TIOJ] 1290. F.不定向邊

題目連結: http://tioj.infor.org/problems/1290 不難發現在走最短路徑的時候不可能會同一邊走兩次(或以上),所以本題其實直接寫個dijkstra就好了XD #include <stdio.h> #include <vector> #include <queue> #include <bitset> typedef unsigned long long llu; using namespace std; struct node{ int to; llu val; }; struct dian{ int me; llu val; }; class cmp{ public: bool operator ()(dian a, dian b){ return a.val > b.val; } }; vector<node> graph[1000 + 10]; bitset<1000 + 10> poped; llu dis [1000 + 10]; int main(){ int V, E; while(scanf("%d %d",&V,&E)!=EOF){ priority_queue<dian,vector<dian>,cmp> pq; poped.reset(); for(int i=0;i<1010;i++){ graph[i].clear(); dis[i] = 2147483648; } int start, end; s...

[TIOJ]1509. 地道問題

題目連結: http://tioj.infor.org/problems/1509 裸單點源最短路徑,直接寫個dijkstra就好了 #include <stdio.h> #include <vector> #include <queue> #include <bitset> #include <algorithm> using std::vector; using std::priority_queue; using std::bitset; using std::swap; struct node{ int vertex; int dis; }; class cmp{ public: bool operator ()(node a, node b){ return a.dis>b.dis; } }; bitset<1000000 + 10> poped; vector<node> forward[1000000 + 10], backward[1000000 + 10]; int dis[1000000 + 10]; priority_queue<node,vector<node>,cmp> pq; int main(){ //input int m,n; unsigned long long ans=0; scanf("%d %d",&m,&n); for(int i=0;i<n;i++){ node OAO; int a; scanf("%d %d %d",&a,&OAO.vertex,&OAO.dis); forwa...