1003 Emergency (25)

版权声明:此博客用作笔记,内容有原创也有借鉴其他博主,开放-学习-借鉴 https://blog.csdn.net/chushoufengli/article/details/80500606
// Dijkstra+DFS
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN = 520;
const int INF = 1000000000;
int G[MAXN][MAXN], N, M, st, ed;
bool vis[MAXN];
int d[MAXN];
int weight[MAXN];
vector<int> pre[MAXN], tempPath, path;
int num = 0, maxWeightSum = 0;
void Dijkstra(int s) {
	fill(d, d + MAXN, INF);
	d[s] = 0;
	for(int i=0; i<N; i++) {
		int u = -1, MIN = INF;
		for(int j=0; j<N; j++) {  //寻找d最小的中间点u
			if(d[j] < MIN && !vis[j]) {
				u = j;
				MIN = d[u];
			}
		}
		if(u == -1) return;
		vis[u] = true;
		for(int v=0; v<N; v++) {  //以u为中间点,为与u相连的v们的d赋值
			if(!vis[v] && G[u][v] != INF) {
				if(d[u] + G[u][v] < d[v]) {
					d[v] = d[u] + G[u][v];  //更新
					pre[v].clear();
					pre[v].push_back(u);				
				} else if(d[u] + G[u][v] == d[v]) {
					pre[v].push_back(u);	
				}
			}
		}
	}
}
void DFS(int v) {
	if(v == st) {
		num++;
		tempPath.push_back(v);
		int tempValueWeight = 0;
		for(int i=0; i<tempPath.size(); i++)
			tempValueWeight += weight[tempPath[i]];
		if(tempValueWeight > maxWeightSum) maxWeightSum = tempValueWeight;
		tempPath.pop_back();
		return;
	}
	tempPath.push_back(v);
	for(int i=0; i<pre[v].size(); i++) DFS(pre[v][i]);
	tempPath.pop_back();
}
int main() {
	scanf("%d%d%d%d",&N, &M, &st, &ed);
	fill(G[0], G[0] + MAXN * MAXN, INF);
	for(int i=0; i<N; i++)
		scanf("%d",&weight[i]);
	for(int i=0; i<M; i++) {
		int C1, C2;
		scanf("%d%d",&C1,&C2);
		scanf("%d",&G[C1][C2]);
		G[C2][C1] = G[C1][C2];  //无向图
	}
	Dijkstra(st);
	DFS(ed);
	printf("%d %d\n", num, maxWeightSum);
	return 0;
}
// 内置数组
#include <cstdio>
using namespace std;
const int MAXN = 520;
const int INF = 1000000000;
int G[MAXN][MAXN], N, M, st, ed;
bool vis[MAXN];
int d[MAXN];
int weight[MAXN], numberPathsPerPoint[MAXN], maxWeightSumPerPoint[MAXN];
void Dijkstra(int s) {
	for(int i=0; i<N; i++) d[i] = INF;
	d[s] = 0;
	numberPathsPerPoint[s] = 1;
	maxWeightSumPerPoint[s] = weight[s];
	for(int i=0; i<N; i++) {
		int u = -1, MIN = INF;
		for(int j=0; j<N; j++) {
			if(d[j] < MIN && !vis[j]) {
				u = j;
				MIN = d[u];
			}
		}
		if(u == -1) return;
		vis[u] = true;
		for(int v=0; v<N; v++) {
			if(!vis[v] && G[u][v] != INF) {
				if(d[u] + G[u][v] < d[v]) {
					d[v] = d[u] + G[u][v];  //更新
					numberPathsPerPoint[v] = numberPathsPerPoint[u];  ////		
					maxWeightSumPerPoint[v] = maxWeightSumPerPoint[u] + weight[v];	
				} else if(d[u] + G[u][v] == d[v]) {
					numberPathsPerPoint[v] += numberPathsPerPoint[u];  ////
					if(maxWeightSumPerPoint[u] + weight[v] > maxWeightSumPerPoint[v])
						maxWeightSumPerPoint[v] = maxWeightSumPerPoint[u] + weight[v];
				}
			}
		}
	}
}
int main() {
	scanf("%d%d%d%d",&N, &M, &st, &ed);
	for(int i=0; i<N; i++)
		for(int j=0; j<N; j++)
			G[i][j] = INF;
	for(int i=0; i<N; i++)
		scanf("%d",&weight[i]);
	for(int i=0; i<M; i++) {
		int C1, C2;
		scanf("%d%d",&C1,&C2);
		scanf("%d",&G[C1][C2]);
		G[C2][C1] = G[C1][C2];
	}
	Dijkstra(st);
	printf("%d %d\n",numberPathsPerPoint[ed], maxWeightSumPerPoint[ed]);
	return 0;
}

展开阅读全文

没有更多推荐了,返回首页