题目链接
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
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
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
题意:
N个城市,M条路,给定N个城市中的救援人员个数,M条连接城市的路和对应距离,求C1到C2救援的最短路共有多少条,有多少救援人员。
思路:最短路
1.dfs
2.dijkstra算法
代码【dfs】:
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int N = 5e2+5;
const ll INF = 0x3f3f3f3f;
int num[N];
int numShorPath,MaxAmou,d=INF;
struct Edge {
int v,w;
Edge() {}
Edge(int v,int w):v(v),w(w) {}
};
vector g[N];
bool vis[N];
void init() {
for(int i=0; i0;
}
}
void dfs(int u,int v,int dist,int MA) {
if(u==v) {
if(dist==d) {
numShorPath++;
if(MA>MaxAmou) {
MaxAmou=MA;
}
} else if(dist1;
d=dist;
MaxAmou=MA;
}
return ;
}
for(int i=0; iint to=next.v,w=next.w;
if(vis[to])
continue;
vis[to]=1;
dfs(to,v,dist+w,MA+num[to]);
vis[to]=0;
}
}
int main() {
int n,m,C1,C2;
cin>>n>>m>>C1>>C2;
for(int i=0; icin>>num[i];
init();
for(int i=0; iint u,v,w;
cin>>u>>v>>w;
g[u].push_back({v,w});
g[v].push_back({u,w});
}
dfs(C1,C2,0,num[C1]);
cout<" "<return 0;
}
2.dijkstra算法
代码:
#include
using namespace std;
#define rep(i,a,n) for(int i=a;i
#define drep(i,n,a) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a,n,sizeof(a))
#define lowbit(i) ((i)&(-i))
typedef long long ll;
typedef unsigned long long ull;
const int INF=0x3f3f3f3f;
const double eps = 1e-6;
const int N = 5e2+5;
int n,m,s,e;
int G[N][N],num[N];
int pathNum[N],maxNum[N];///pathNum存储七点到所有点的路径数
///maxNum存储起点到所有点的最大人手
int dis[N];///dis存储从起点到所有点的最短路径的边权
bool vis[N];
void dijkstra() {
fill(dis,dis+N,INF);
dis[s]=0,pathNum[s]=1,maxNum[s]=num[s];
while(1) {
/*不断更新起点到其他点的最短路径,并更新最短路径中的点权*/
int minD=INF,v=-1;
for(int i=0; iif (!vis[i]&&dis[i]if(v==-1) break;
vis[v]=1;
for(int i=0; iif(vis[i]||G[v][i]==INF) continue;///无法到达或者已经访问
int tmpDis=dis[v]+G[v][i];
int tmpNum=maxNum[v]+num[i];
if(tmpDiselse if(tmpDis==dis[i]) {
pathNum[i]+=pathNum[v];
if(tmpNum>maxNum[i]) {
maxNum[i]=tmpNum;
}
}
}
}
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&e);
for(int i=0; iscanf("%d",&num[i]);
}
fill(G[0],G[0]+N*N,INF);
for(int i=0; iint u,v,w;
scanf("%d%d%d",&u,&v,&w);
G[u][v]=G[v][u]=w;
}
dijkstra();
printf("%d %d",pathNum[e],maxNum[e]);
return 0;
}