Dijkstra
TchongS1010C · · 个人记录
适用于一个起点,多个终点的最短路算法(其实还有Bell-Ford)
设图为
- 我们先把
G 的点搞成两种(第一种表示还没有最短路) - 在所有第一种的点中,找到与
s 距离最短的点x 搞成第二种 - 用
x 做中转点,更新x 的邻接点y 的最短路dis[y] = min(dis[y] , dis[x] + w) ; - 重复
2.,3.
时间复杂度:
#include <bits/stdc++.h>
using namespace std ;
long long n , m , s , u , v , w ;
struct node
{
int y , w ;
} ;
vector <node> vx[10005] ;
bool color[10005] ;
long long f[10005] ;
void dsj(int st)
{
for(int i = 1 ; i <= n ; i ++)
{
f[i] = INT_MAX ;
color[i] = 0 ;
}
f[st] = 0 ;
for(int i = 1 ; i <= n ; i ++)
{
long long minn = LLONG_MAX ;
int x = 0 ;
for(int j = 1 ; j <= n ; j ++)
{
if(f[j] <= minn && !color[j])
{
minn = f[j] ;
x = j ;
}
}
color[x] = 1 ;
for(int j = 0 ; j < vx[x].size(); j ++)
{
f[vx[x][j].y] = min(f[x] + vx[x][j].w , f[vx[x][j].y]);
}
}
}
int main()
{
cin >> n >> m >> s ;
for(int i = 1 ; i <= m ; i ++)
{
cin >> u >> v >> w ;
vx[u].push_back({v , w}) ;
}
dsj(s) ;
for(int i = 1 ; i <= n ; i ++)
{
cout << f[i] << " " ;
}
return 0 ;
}
P4799
如果我们直接把代码套进去会RE,然而如果改完数组大小,会TLE(
我们浪费时间的主要地是要找最近的第一种的点:距离最少?可以用优先队列来优化,但是priority_queue <node> pq怎么排序?那就要用重载运算符了(暂时只用得到这一种,背下来就好)
bool operator < (const node & tmp) const
{
return w > tmp.w
}
#include <bits/stdc++.h>
using namespace std ;
long long n , m , s , u , v , w ;
struct node
{
int y , w ;
} ;
vector <node> vx[200005] ;
bool color[200005] ;
long long f[200005] ;
void dsj(int st)
{
for(int i = 1 ; i <= n ; i ++)
{
f[i] = INT_MAX ;
color[i] = 0 ;
}
f[st] = 0 ;
for(int i = 1 ; i <= n ; i ++)
{
long long minn = LLONG_MAX ;
int x = 0 ;
for(int j = 1 ; j <= n ; j ++)
{
if(f[j] <= minn && !color[j])
{
minn = f[j] ;
x = j ;
}
}
color[x] = 1 ;
for(int j = 0 ; j < vx[x].size(); j ++)
{
f[vx[x][j].y] = min(f[x] + vx[x][j].w , f[vx[x][j].y]);
}
}
}
int main()
{
cin >> n >> m >> s ;
for(int i = 1 ; i <= m ; i ++)
{
cin >> u >> v >> w ;
vx[u].push_back({v , w}) ;
}
dsj(s) ;
for(int i = 1 ; i <= n ; i ++)
{
cout << f[i] << " " ;
}
return 0 ;
}
P1629
我们要从
- 起点
1 ,终点2\to n :原图dijkstra - 起点
2 \to n ,终点1 ,反图dijkstra#include <bits/stdc++.h> using namespace std; int n, m , dis[1005] , dis1[1005] , ans , x , y , w ; bool vis[1005]; struct node { int x, w; bool operator < (const node & tmp) const { return w > tmp.w; } }; vector<node> v[1005]; vector<node> v1[1005]; void dijkstra(int dis[], vector<node> v[]) { priority_queue<node> pq; memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { dis[i] = 1e9; } dis[1] = 0; pq.push({1, 0}); while (!pq.empty()) { int x = pq.top().x; pq.pop(); if (vis[x]) { continue; } vis[x] = true; for (int i = 0; i < v[x].size(); i++) { int y = v[x][i].x; int w = v[x][i].w; if (dis[x] + w < dis[y]) { dis[y] = dis[x] + w; pq.push({y, dis[y]}); } } } } int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> x >> y >> w; v[x].push_back({y, w}); v1[y].push_back({x, w}); } dijkstra(dis, v); dijkstra(dis1, v1); for (int i = 1; i <= n; i++) { ans += dis[i] + dis1[i]; } cout << ans; return 0; }p1576
- 我们可以把
1- 续费率=到手率当作边权 - 这样子就跟普通的dijkstra一样了,除了运算符是
\times 不是+ ,并且还是最大路(dis_a=1 ) - 最后答案是
1/dis_b #include<bits/stdc++.h> using namespace std; int n , m , a , b , x , y; double z , dp[2005] ; bool f[2005] ; struct node { int x; double w; bool operator < (const node & tmp) const { return w < tmp.w ; } } ; vector<node> v[2005] ; void dijkstra() { priority_queue<node> pq ; memset(f , 0 , sizeof f) ; dp[a] = 1.0 ; pq.push({a , 1.0}) ; while(pq.empty() == false) { int x = pq.top().x ; pq.pop() ; if(f[x] == true) continue ; f[x] = true; for(int i = 0 ; i < v[x].size() ; i++) { int y = v[x][i].x; double w = v[x][i].w; if(dp[x] * w > dp[y]) { dp[y] = dp[x]*w; pq.push({y , dp[y]}); } } } } int main() { cin >> n >> m ; for(int i = 1 ; i <= m ; i ++) { cin >> x >> y >> z ; v[x].push_back({y , 1 - z / 100}) ; v[y].push_back({x , 1 - z / 100}) ; } cin >> a >> b ; dijkstra() ; printf("%.8lf" , 100 / dp[b]) ; return 0; }