您的位置:首页 > 其它

L2-001. 紧急救援 (dijkstra算法)

2018-01-21 13:25 351 查看


L2-001. 紧急救援

时间限制

200 ms

内存限制

65536 kB

代码长度限制

8000 B

判题程序

Standard

作者

陈越

作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。

输入格式:

输入第一行给出4个正整数N、M、S、D,其中N(2<=N<=500)是城市的个数,顺便假设城市的编号为0~(N-1);M是快速道路的条数;S是出发地的城市编号;D是目的地的城市编号。第二行给出N个正整数,其中第i个数是第i个城市的救援队的数目,数字间以空格分隔。随后的M行中,每行给出一条快速道路的信息,分别是:城市1、城市2、快速道路的长度,中间用空格分开,数字均为整数且不超过500。输入保证救援可行且最优解唯一。

输出格式:

第一行输出不同的最短路径的条数和能够召集的最多的救援队数量。第二行输出从S到D的路径中经过的城市编号。数字间以空格分隔,输出首尾不能有多余空格。
输入样例:
4 5 0 3
20 30 40 10
0 1 1
1 3 2
0 3 3
0 2 2
2 3 2

输出样例:
2 60
0 1 3


// 第一种,借用了dijkstra框架
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#define INF 1 << 30
using namespace std;

int Weight[500] = { 0 }; // 每个城市的救援队数量
int totWeight[500] = { 0 };
int pathAmount[500] = { 0 };
int G[510][510];
int path[510];
int dis[510];
int vis[510];
int N, M, S, D;
int num;

void dijkstra(int s)
{
for (int i = 0; i < N; i++)
dis[i] = INF;
vis[s] = true;
dis[s] = 0;

for (int i = 0; i < N; i++)
{
if (G[s][i] != INF && i != s)
{
dis[i] = G[s][i];
path[i] = s;
totWeight[i] = Weight[s] + Weight[i];
pathAmount[i] = 1;
}
}

for (int i = 0; i < N - 1; i++)
{
int min = INF, u = -1;
for (int j = 0; j < N; j++)
{
if (!vis[j] && dis[j] < min)
{
min = dis[j];
u = j;
}
}
vis[u] = true;
for (int j = 0; j < N; j++)
{
if (!vis[j])
{
if (dis[u] + G[u][j] < dis[j])
{
dis[j] = dis[u] + G[u][j];
path[j] = u;
totWeight[j] = totWeight[u] + Weight[j];
pathAmount[j] = pathAmount[u];
}
else if (dis[u] + G[u][j] == dis[j])
{
pathAmount[j] += pathAmount[u];
if (totWeight[j] < totWeight[u] + Weight[j])
{
totWeight[j] = totWeight[u] + Weight[j];
path[j] = u;
}
}
}
}
}
}

int main()
{
//freopen("data.txt", "r", stdin);
int re[510];
cin >> N >> M >> S >> D;
for (int i = 0; i < N; i++)
{
cin >> num;
Weight[i] = num;
}
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
G[i][j] = INF;
for (int i = 0; i < M; i++)
{
int start, end, length;
cin >> start >> end >> length;
if (start != end)
{
G[start][end] = min(G[start][end], length);
G[end][start] = G[start][end];
}
}
dijkstra(S);
int num = 0, cur = D;
while (cur != S)
{
re[num++] = cur;
cur = path[cur];
}
re[num++] = S;
cout << pathAmount[D] << " " << totWeight[D] << endl;
for (int i = num - 1; i > 0; i--)
cout << re[i] << " ";
cout << re[0];
return 0;
}



// 第二种,使用了DFS,比第一种容易理解,可惜超时
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
struct node {
int end, length;
};

// 为了方便,定义为全局变量
int minLen = 1 << 30; // 最短路径的长度
int minRodeAmount = 1; // 最短路径的数量
int maxPeopleAmount = -1; // 最多救援队数量
int totalLen = 0; // 目前走过经过的长度
int totalPeople = 0;// 目前召集的救援队的数量
int visited[500] = { 0 }; // 城市访问标记
int Weight[500] = { 0 }; // 每个城市的救援队数量
int path[500]; // 访问路径
int min_path[500]
add5
; // 最短路径
int minL[500][250000] = { 0 };
int min_depth = 0;
int depth = 0; // 访问深度
vector< vector <node> > G(510); // 城市的路径
int N, M, S, D;
int num;
int start, end;

void Dfs(int v)
{
if (v == D) {
path[depth++] = v;
if (minLen < totalLen)
return;
else if (minLen > totalLen) {
minRodeAmount = 1;
minLen = totalLen;
memcpy(min_path, path, depth * sizeof(int));
min_depth = depth;
maxPeopleAmount = totalPeople;
}
else if (minLen == totalLen) {
minRodeAmount++;
if (maxPeopleAmount < totalPeople) {
memcpy(min_path, path, depth * sizeof(int));
min_depth = depth;
maxPeopleAmount = totalPeople;
}
}
return;
}

path[depth++] = v;
for (int i = 0; i < G[v].size(); i++) {
if (!visited[G[v][i].end]) {

if (totalLen + G[v][i].length > minLen)
continue;
if (totalPeople + Weight[G[v][i].end] <= minL[G[v][i].end][totalLen + G[v][i].length]) {
minRodeAmount++;  continue;
}
else
minL[G[v][i].end][totalLen + G[v][i].length] = totalPeople + Weight[G[v][i].end];
totalLen += G[v][i].length;
totalPeople += Weight[G[v][i].end];
visited[v] = 1;
Dfs(G[v][i].end);
totalLen -= G[v][i].length;
totalPeople -= Weight[G[v][i].end];
visited[v] = 0;
depth--;
}
}
return;
}

int main()
{
//freopen("data.txt", "r", stdin);
cin >> N >> M >> S >> D;
for (int i = 0; i < N; i++)
{
cin >> num;
Weight[i] = num;
}
for (int i = 0; i < M; i++)
{
node n;
int end, length;
cin >> start >> end >> length;
if (start != end)
{
n.end = end; n.length = length;
G[start].push_back(n);
n.end = start; n.length = length;
G[end].push_back(n);
}
}
totalPeople += Weight[S];
Dfs(S);
if (min_depth > 0) {
cout << minRodeAmount << " " << maxPeopleAmount << endl;
for (int i = 0; i < min_depth; i++) {
if (!i)
cout << min_path[i];
else
cout << " " << min_path[i];
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: