1003 Emergency (25 分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.
Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
Sample Input:
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
Sample Output:
2 4
使用Dijkstra算法的经典题目,要输出最短路径的数量和能够召集的救护队的最多数量,只需在原有Dijkstra算法上做适当修改即可。
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef struct Edge
{
int next,len;
}Edge;
vector<Edge> e[1001];
bool mark[1001];
int dist[1001];
int res[1001];
int teamNum[1001];
int pathNum[1001];
int main(){
int n,m,c1,c2;
scanf("%d %d %d %d",&n,&m,&c1,&c2);
for(int i=0;i<n;i++)
{
scanf("%d",&res[i]);
}
for(int i=0;i<m;i++)
{
int a,b,l;
scanf("%d %d %d",&a,&b,&l);
struct Edge e1,e2;
e1.next=a;
e1.len=l;
e2.next=b;
e2.len=l;
e[a].push_back(e2);
e[b].push_back(e1);
}
for(int i=0;i<n;i++)
{
dist[i]=-1;
mark[i]=0;
}
dist[c1]=0;
mark[c1]=1;
int newp=c1;
teamNum[c1]=res[c1];
pathNum[c1]=1;
for(int i=0;i<n;i++) //循环n次,n个点
{
for(int j=0;j<e[newp].size();j++) //循环新加入点直接相邻的边
{
int tmp=e[newp][j].next; //该边另一个结点
if(mark[tmp]==0&&(dist[tmp]==-1||dist[tmp]>dist[newp]+e[newp][j].len)) //不要漏掉dist[tmp]==-1的情况,因为所有边初始化是0
{
dist[tmp]=dist[newp]+e[newp][j].len;
pathNum[tmp]=pathNum[newp];更新最短路径数量
teamNum[tmp]=teamNum[newp]+res[tmp];更新城市的救护队数量
}
else if(dist[tmp]==dist[newp]+e[newp][j].len)
{
pathNum[tmp]+=pathNum[newp]; 增加最短路径数量
teamNum[tmp]=max(teamNum[tmp],teamNum[newp]+res[tmp]);找出能够召集最多的城市救护队数量
}
}
int min=999999;
for(int j=0;j<n;j++)
{
if(dist[j]==-1)
continue;
if(mark[j]==0&&dist[j]<min)
{
min=dist[j];
newp=j;
}
}
mark[newp]=1;
}
printf("%d %d",pathNum[c2],teamNum[c2]);
}
1030 Travel Plan (30 分)
https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input:
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output:
0 2 3 3 40
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
int newP;
typedef struct Edge
{
int next,cost,path;
}Edge;
vector<Edge>v[1001];
int mark[1001];
int dis[1001];
int cost[1001];
int pre[1001];
int n;
vector<int>path;
int s;
void Dijkstra(int s)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<v[newP].size();j++)
{
Edge t=v[newP][j];
int n2=t.next;
if(mark[n2]==0&&(dis[n2]==-1||dis[n2]>dis[newP]+t.path||(dis[n2]==dis[newP]+t.path&&cost[n2]>cost[newP]+t.cost)))
{
dis[n2]=dis[newP]+t.path;
cost[n2]=cost[newP]+t.cost;
pre[n2]=newP;
//printf("%d ",dis[n2]);
}
}
int min=9999999;
//printf("%d ",newP);
for(int j=0;j<n;j++)
{
if(dis[j]==-1)
continue;
if(mark[j]==0&&dis[j]<min)
{
min=dis[j];
newP=j;
}
}
mark[newP]=1;
}
}
void DFS(int v) //Dijkstra+DFS!!!使用Dijkstar算法求出符合要求的最短路径,利用DFS算法输出该路径即可
{
if(v==s)
{
path.push_back(v);
return;
}
DFS(pre[v]);
path.push_back(v);
}
int main(){
int m,d;
scanf("%d %d %d %d",&n,&m,&s,&d);
for(int i=0;i<m;i++)
{
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
struct Edge tmp;
tmp.next=b;
tmp.path=c;
tmp.cost=d;
v[a].push_back(tmp);
tmp.next=a;
v[b].push_back(tmp);
}
for(int i=0;i<n;i++)
{
dis[i]=-1;
mark[i]=0;
cost[i]=0;
}
dis[s]=0;
mark[s]=1;
newP=s;
Dijkstra(s);
DFS(d);
for (int i = 0; i < path.size(); i++)
{
printf("%d ", path[i]);
}
printf("%d %d",dis[d],cost[d]);
}