DIJKSTRA(G, w, s) 1 INITIALIZE-SINGLE-SOURCE(G, s) 2 S <- Ø 3 Q <- V[G] 4 while Q ≠ Ø 5 do u <- EXTRACT-MIN(Q) 6 S <- S ∪ {u} 7 for each vertex v ∈ Adj[u] 8 do RELAX(u,v,w)
#include<bits/stdc++.h> #define ll long long #define int long long #define ios ios::sync_with_stdio(0),cin.tie(0) #define N 105 #define FOR(i,n) for(int i=0;i<n;i++) #define pii pair<int,int> usingnamespace std;
voidsolve(){ vector<pii> edge[N]; //存圖 int n,m,s,e,f;cin>>n>>m>>s>>e>>f; int dis[N];fill(dis,dis+N,1e16); bool visit[N]; //城市數n、方案數m、s起、e終、f箱數 memset(visit,0,sizeof(visit)); for(int i=0;i<m;i++){ int a,b,c,d,e;cin>>a>>b>>c>>d>>e; //一條由a連到b的邊,權重c,流量超過d,則改權重c int val = (f>d?c*d+e*(f-d):c*f); edge[a].push_back({b,val}); } //Dijkstra priority_queue<pii,vector<pii>,greater<pii>> pq; //(距離,點) pq.push({0,s}); dis[s] = 0; while(!pq.empty()){ int cur = pq.top().second; pq.pop(); if(visit[cur])continue; for(auto i:edge[cur]){ int next = i.first,weight = i.second; if(dis[cur]+weight<dis[next]){ dis[next] = dis[cur]+weight; pq.push({dis[next],next}); } } visit[cur] = 1; } cout<<dis[e]<<endl; }
signedmain(){ ios; int t;cin>>t; while(t--){ solve(); } }
BELLMAN-FORD(G,w,s) 1 INITIALIZE-SINGLE-SOURCE(G,s) 2 for i <- 1 to |V[G]|-1 3 do for each edge (u,v)∈ E[G] 4 do RELAX(u,v,w) 5 for each edge (u,v)∈ E[G] 6 do if d[v] > d[u]+w(u,v) 7 then return FALSE 8 return TRUE
Shortest-Path-Faster-Algorithm(G, s) 1 for each vertex v ≠ s in V(G) 2 d(v) := ∞ 3 d(s) := 0 4 offer s into Q 5 while Q is not empty 6 u := poll Q 7 for each edge (u, v) in E(G) 8 if d(u) + w(u, v) < d(v) then 9 d(v) := d(u) + w(u, v) 10 if v is not in Q then 11 offer v into Q
#include<bits/stdc++.h> #define ll long long #define int long long #define ios ios::sync_with_stdio(0),cin.tie(0) #define N 105 #define FOR(i,n) for(int i=0;i<n;i++) #define pii pair<int,int> usingnamespace std;
voidsolve(){ vector<pii> edge[N]; //存圖 int n,m,s,e,f;cin>>n>>m>>s>>e>>f; int dis[N];fill(dis,dis+N,1e16); bool visit[N]; //城市數n、方案數m、s起、e終、f箱數 memset(visit,0,sizeof(visit)); for(int i=0;i<m;i++){ int a,b,c,d,e;cin>>a>>b>>c>>d>>e; //一條由a連到b的邊,權重c,流量超過d,則改權重c int val = (f>d?c*d+e*(f-d):c*f); edge[a].push_back({b,val}); } //SPFA queue<int> que; que.push(s); dis[s] = 0; visit[s] = 1; while(!que.empty()){ int cur = que.front(); que.pop(); visit[cur] = 0; //pop出來將狀態改成不在queue中 for(auto i:edge[cur]){ int next = i.first,weight = i.second; if(dis[next] > dis[cur]+weight){ dis[next] = dis[cur]+weight; if(!visit[next])que.push(next); } } } cout<<dis[e]<<endl; }
signedmain(){ ios; int t;cin>>t; while(t--){ solve(); } }
#include<bits/stdc++.h> #define ll long long #define int long long #define Orz ios::sync_with_stdio(0),cin.tie(0) #define N 2003 #define FOR(i,n) for(int i=0;i<n;i++) #define pii pair<int,int> usingnamespace std; int n,m,dis[N][N]; int dx[4] = {0,-1,0,1},dy[4] = {1,0,-1,0}; bool visit[N][N],maze[N][N];
#include<bits/stdc++.h> #define ll long long #define int long long #define Orz ios::sync_with_stdio(0),cin.tie(0) #define N 200005 #define FOR(i,n) for(int i=0;i<n;i++) #define pii pair<int,int> usingnamespace std; int n,m,q; bool visit[N]; vector<pii> edge[2][N]; //edge[0]->normal,edge[1]->opposite
#include<bits/stdc++.h> #define ll long long #define int long long #define N 10005 #define FOR(i,n) for(int i=0;i<n;i++) #define pii pair<int,int> #define pid pair<int,double> #define pdi pair<double,int> usingnamespace std; int n,m,s,t; double dis[N]; bool visit[N]; vector<pid> edge[N];
voidsolve(){ memset(visit,0,sizeof(visit)); scanf("%lld %lld %lld %lld",&n,&m,&s,&t); for(int i=0;i<m;i++){ int a,b;double c;scanf("%lld %lld %lf",&a,&b,&c); edge[a].push_back({b,(double)log10(c+1)}); } fill(dis,dis+n+2,1e16); priority_queue<pdi,vector<pdi>,greater<pdi>> pq; dis[s] = 0.0; pq.push({0,s}); while(!pq.empty()){ int cur = pq.top().second; pq.pop(); if(visit[cur])continue; visit[cur] = 1; for(auto i:edge[cur]){ int next = i.first; double w = i.second; if(dis[next] > dis[cur]+w){ dis[next] = dis[cur]+w; pq.push({dis[next],next}); } } } double ans = dis[t]; int x = floor(ans); ans-=x; printf("%.2fe+%03lld\n",pow(10,ans),x); }
signedmain(){ int t;t = 1; while(t--){ solve(); } }
#include<bits/stdc++.h> #define ll long long #define int long long #define Orz ios::sync_with_stdio(0),cin.tie(0) #define N 101 #define FOR(i,n) for(int i=0;i<n;i++) #define pii pair<int,int> #define pid pair<int,double> #define pdi pair<double,int> usingnamespace std; int n,dp[N][N];
#include<bits/stdc++.h> #define ll long long #define int long long #define double long double #define N 14 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> usingnamespace std; int n,m; vector<pii> edge[N]; int dis[N],dp[N][1<<N],pre[N][1<<N]; bool visit[N]; //定義dp[i][j]為現在在點i,拜訪過點集合j(i不在點集j中)
#include<bits/stdc++.h> #define ll long long #define int long long #define double long double #define N 1005 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define rrep(i,l,r) for(int i=l;i<r;i++) #define pii pair<int,int> #define x first #define y second usingnamespace std; int n,row,col,mp[N][N],dis[N][N]; int s1,s2,e1,e2; int dx[4] = {0,-1,0,1},dy[4] = {1,0,-1,0}; bool visit[N][N]; typedef pair<int ,pair<int,int>> pp;
#include<bits/stdc++.h> #define ll long long #define int long long #define double long double #define N 105 #define Orz ios::sync_with_stdio(0),cin.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define rrep(i,l,r) for(int i=l;i<r;i++) #define pii pair<int,int> #define pif pair<int,float> #define x first #define y second usingnamespace std; int n,k,visit[20];