PAT 1003 emergency
一. dijkstra算法
伪代码:
设d[0]=0, 其他d[i]=INF
循环n次{
在所有为标号结点中选出d值最小结点x
给结点x标记
对于x出发的所有边(x,y), 更新d[y]=min{d[y], d[x]+w(x,y)}
}
关键步骤:
循环内主要分为两部分
- 找出当前结点的最短的路径
- 根据找出的最短路径的结点, 来更新dist表: 若该结点相邻的边比原来小,则更新
此题代码
#include <stdio.h>
#define INF 99999999
#define MAX 501
int src, tar;
void dijkstra(int g[][MAX], int n,int team[MAX]){
int x, y, i, m;
int dist[MAX];
int v[MAX];
int count[MAX];
int dt[MAX]; //src到当前路径的最大队伍数
for(i=0; i < n; i++){
v[i] = 0;
dt[i] = team[src];
count[i] = 1;
dist[i] = g[src][i];
if(dist[i] != INF && i != src){ //此处特别注意!!!(i=src)改了 30分钟, 第二个案例过不了
dt[i] = team[i]+team[src];
}
}
count[src] = 1;
v[src] = 1;
for(i=0; i < n; i++){
m = INF;
for(y=0; y < n; y++){ //得到权值最小的边
if(!v[y] && dist[y] <= m){
m = dist[x=y];
}
}
if(m == INF || x == tar) break;
v[x] = 1;
for(y=0; y < n; y++){ //更新dist
if(dist[x]+g[x][y] < dist[y]){
dist[y] = dist[x]+g[x][y];
dt[y] = dt[x] + team[y];
count[y] = count[x];
}else if(dist[x]+g[x][y] == dist[y]){
if (dt[x] + team[y] > dt[y])
dt[y] = dt[x] + team[y];
count[y] += count[x];
}
}
}
printf("%d %d",count[tar], dt[tar]);
}
int main(void){
int n, m;
scanf("%d %d %d %d", &n, &m, &src, &tar);
int i, j;
int team[MAX];
int g[MAX][MAX];
for(i = 0;i < n; i++){
for(j = 0; j < n; j++){
g[i][j] = INF;
}
}
g[src][src] = 0;
for(i =0; i < n; i++){
scanf("%d", &team[i]);
}
int t,k,w;
for(i = 0;i < m; i++){
scanf("%d %d %d", &t, &k, &w);
g[t][k] = g[k][t] = w;
}
dijkstra(g, n, team);
return 0;
}
记录路径
- 从终点出发, 通过d[i] + w[i][j] == d[j] 不断从 j 追溯到 i
- 空间换时间, 在更新数组dist时, 记录其父亲指针, 具体的把
改成:d[y] = min{d[y] + w[x][y], d[y]}
if(d[y] > d[x] + w[x][y]){ d[y] = d[x] + w[x][y]; fa[y] = x; // 记录父亲指针的数组 }
时间复杂度分析
显然, 两个, 循环体一共执行了n次, 每次循环中的操作:求最小dist;更新dist表, 均是O(N),所以复杂度
为O( n^2)
算法改进–松弛操作
数据结构: 邻接表, 优先队列
思想:
1. 求当前结点最短边的操作–即求最小dist–直接Q.pop();
2. 把更新dist表改成遍历结点的所有临边, 时间复杂度降为边数 m, 若找到更短边, 把该边对应的结点压入有先队列, 时间复杂度最多为 logn (实际不会达到);
3. 最外层遍历终止条件: 优先队列不为空
主要代码:
void dijkstra(int s){
priority_queue<HeapNode> Q; //按照离源点距离最小的顶点(包括中间会经过其他顶点的)排序
for(int i =0; i <n; i++) d[i] = INF;
d[s] = 0;
memset(done, 0, sizeof(done)); //done是已访问过的顶点
while(!Q.empty()){
HeapNode x = Q.top(); Q.pop();
int u = x.u;
if(done[u])
continue;
for(int i = 0; i < G[u].size; i++){ //G[u].size 是u的所有邻边
Edge& e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist){ //e.to是e边的出顶点
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
Q.push((HeapNode){d[e.to], e.to});
}
}
}
}
时间复杂度为 mlogn, 因为大部分稀疏矩阵, 所以m的值不会太大
二. DFS 算法
注意剪枝
import java.util.Arrays;
import java.util.Scanner;
public class Main{
static int INF = 999999999;
static boolean[] visited;
static int path;
static int team;
static int len = INF;
static int n;
static int m;
static int src, tar;
static int[] city;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); // citiy_num
m = sc.nextInt(); // rods_num
src = sc.nextInt();
tar = sc.nextInt();
int i;
city = new int[n];
int[][] g = new int[n][n];
for (i = 0; i < n; i++)
Arrays.fill(g[i], INF);
visited = new boolean[n];
for (i = 0; i < n; i++) {
city[i] = sc.nextInt();
g[i][i] = 0;
visited[i] = false;
}
for (i = 0; i < m; i++) {
int r = sc.nextInt();
int c = sc.nextInt();
int w = sc.nextInt();
g[r][c] = w;
g[c][r] = w;
}
sc.close();
visited[src] = true;
Dfs(g, src, city[src], 0);
System.out.printf("%d %d", path, team);
}
public static void Dfs(int[][] g,int cur, int teamCount, int curLen){
if(curLen > len){
return ;
}
if(cur == tar){
if(curLen < len){
len = curLen;
path = 1;
team = teamCount;
}else if (curLen == len){
path ++;
if(team < teamCount){
team = teamCount;
}
}
return ;
}
for(int i = 0; i < n; i++){
if(!visited[i] && g[cur][i] != INF){
visited[i] = true;
Dfs(g, i, teamCount + city[i], curLen+g[cur][i]);
visited[i] = false;
}
}
}
}