赞
踩
我的代码:
- #include<bits/stdc++.h>
- using namespace std;
-
- int main(){
- int num_city;
- int num_road;
- int start;
- int end;
- int Max=-1;
- cin>>num_city>>num_road>>start>>end;
- vector<int> is_vis(num_city,0);
- vector<int> aid(num_city);
- for(int i=0;i<num_city;i++){
- cin>>aid[i];
- }
- int x,y,dis;
- vector<vector<int>>path (num_road,vector<int>(num_road,0));
- for(int i=0;i<num_road;i++){
- cin>>x>>y>>dis;
- path[x][y]=path[y][x]=dis;
- }
- int shortest_path=99999999;
- int res_num=0;
- //int res_sum=0;
- function<void(int ,int,int)> dfs = [&](int node,int length,int sum) {
- if(length>shortest_path) return;
- if(node==end){
- if(length ==shortest_path){res_num++;Max=max(sum,Max);}
- if(length<shortest_path) {res_num=1;shortest_path=length;Max=sum;}
- return;
- }
- for(int i=0;i<num_city;i++){
- if(is_vis[i]==0 && path[i][node]!=0){
- is_vis[i]=1;
- dfs(i,length+path[i][node],sum+aid[i]);
- is_vis[i]=0;
- }
- }
- };
- is_vis[start]=1;
- dfs(start,0,aid[start]);
- cout<<res_num<<" "<<Max;
- return 0;
- }
佬的代码:
- #include<bits/stdc++.h>
- using namespace std;
-
- const int MAX_LEN = 0x3f3f3f3f;
- const int c_maxn = 500+10;
- int G[c_maxn][c_maxn]; //城市地图
- int c_pro[c_maxn]; //每个城市的救火队数量
- int vis[c_maxn]; //-1为有回路、0为正在访问、1为已访问
- int c_n, r_n, sta, fin; //城市数量,道路数量,起点、终点
-
- int Min=MAX_LEN, Max = -1, num_r = 0; //最优路程和救火队数量
-
-
- void dfs(int u, int len, int value) {
- if(len > Min) return; //剪枝
- if(u == fin) {
- if(Min > len) { //如果路程更短,则直接更新
- Min = len; Max = value;
- num_r = 1;
- } else if(Min == len) { //如果路程相等,则比较救火队数量
- Max = max(value, Max);
- num_r++;
- }
- return;
- }
- for(int v = 0; v < c_n; v++) {
- if(G[u][v] != -1) {
- if(vis[v] == 1) continue;
- vis[v] = 1;
- dfs(v, len+G[u][v], value+c_pro[v]);
- vis[v] = 0;
- }
- // cout << u << ' ' << v << ' ' << G[u][v] << '\n';
- }
- }
-
- int main() {
- memset(G, -1, sizeof(G));
- cin >> c_n >> r_n >> sta >> fin;
- for(int i = 0; i < c_n; i++) cin >> c_pro[i];
- for(int i = 0; i < r_n; i++) {
- int x1, x2, v;
- cin >> x1 >> x2 >> v;
- G[x1][x2] = G[x2][x1] = v;
- }
-
- vis[sta] = 1;
- dfs(sta, 0, c_pro[sta]); //起始节点、道路长度、起始救火队数量
- cout << num_r << ' ' << Max << '\n';
- return 0;
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。