城市间紧急救援(25 分)
来源:互联网 发布:手机对话翻译软件 编辑:程序博客网 时间:2024/05/31 13:15
这个题目,一开始用数组统计路径个数,在更新最短路的路径个数时总是错误,后来直接从起点回溯了一遍,就可以了,
更新最短路的时候有两个权值因素,一个是路径长度一个是数目,在路径在同一个结点的时候要选择数目最多的那一个,dij也能写,spfa也可以,都可以试一试
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <queue>#include <math.h>#include <stack>#include <utility>#include <string>#include <sstream>#include <cstdlib>#include <set>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;const int maxn = 500 + 10;int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};struct Edge{ int to; int cost; Edge(int b = 0,int c = 0) { to = b; cost = c; }};vector<Edge>G[maxn];int vis[maxn];int d[maxn];int pre[maxn];int pay[maxn];int num[maxn];int ans[maxn];int n,m,st,en;int sum;int ra;void init(){ sum = 0; for(int i = 0; i <= n+10; i++) { G[i].clear(); } memset(num,0,sizeof(num)); memset(d,INF,sizeof(d)); memset(vis,0,sizeof(vis)); memset(pay,0,sizeof(pay)); memset(pre,-1,sizeof(pre));}void dfs(int u){ if(u == en) { ra++; return; } for(int i = 0; i < G[u].size(); i++) { Edge e = G[u][i]; if(!vis[e.to]&&d[e.to] == d[u] + e.cost) { vis[e.to] = 1; dfs(e.to); vis[e.to] = 0; } }}void spfa(int s){ //cout<<"__________"<<endl; d[s] = 0; vis[s] = 1; pay[s] = num[s]; pre[s] = -1; queue<int>que; que.push(s); while(!que.empty()) { int v = que.front(); que.pop(); vis[v] = 0; for(int i = 0; i < G[i].size(); i++) { Edge e = G[v][i]; if(d[e.to] > d[v] + e.cost) { //cout<<"__________"<<endl; d[e.to] = d[v] + e.cost; pay[e.to] = pay[v] + num[e.to]; pre[e.to] = v; if(!vis[e.to]) { vis[e.to] = 1; que.push(e.to); } } else if(d[e.to] == d[v] + e.cost) { if(pay[e.to] < pay[v] + num[e.to]) { //cout<<"__________"<<endl; pre[e.to] = v; pay[e.to] = pay[v] + num[e.to]; if(!vis[e.to]) { vis[e.to] = 1; que.push(e.to); } } } } } int cnt = 0; ra = 0; memset(vis,0,sizeof(vis)); dfs(st); printf("%d %d\n",ra,pay[en]); int t = en; while(pre[t]!=-1) { ans[cnt++] = t; t = pre[t]; } ans[cnt++] = s; printf("%d",ans[cnt-1]); for(int i = cnt-2; i >=0; i--) { printf(" %d",ans[i]); }}int main(){ scanf("%d %d %d %d",&n,&m,&st,&en); init(); for(int i = 0; i < n; i++) { scanf("%d",&num[i]); } for(int i = 0; i < m; i++) { int x,y,w; scanf("%d %d %d",&x,&y,&w); G[x].push_back(Edge(y,w)); G[y].push_back(Edge(x,w)); } spfa(st); return 0;}
7-1 城市间紧急救援(25 分)
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领你的救援队尽快赶往事发地,同时,一路上召集尽可能多的救援队。
输入格式:
输入第一行给出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 320 30 40 100 1 11 3 20 3 30 2 22 3 2
输出样例:
2 600 1 3
阅读全文
1 0
- 城市间紧急救援(25 分)
- 城市间紧急救援(25 分)
- 7-35 城市间紧急救援(25 分)
- 7-1 城市间紧急救援(25 分)
- 5-35 城市间紧急救援 (25分)
- 5-35 城市间紧急救援 (25分)
- PTA-数据结构 5-35 城市间紧急救援 (25分)
- 城市间紧急救援
- 城市间紧急救援
- 6-08. 城市间紧急救援(25)--Dijkstra
- 5-5 城市间紧急救援 (25分)【最短路spfa】
- 6-08. 城市间紧急救援
- PAT 5-35 城市间紧急救援
- 城市间紧急救援-多维最短路
- PAT 一 城市间紧急救援
- dijkstra模板 pat 城市间紧急救援
- Dijkstra算法-城市间紧急救援
- PTA 5-7 城市间紧急救援 【Dijkstra+记录路径】
- Laravel--ajax--文件上传
- java常见面试题及答案
- Go语言切片的本质
- 微信小程序层级问题
- MVC5中使用Log4net
- 城市间紧急救援(25 分)
- 【学以致用】android功能实现1---分享功能
- 游戏的几大分类
- APP安装完成打开再返回桌面打开启动两次应用的问题
- mysql中查询一个字段在哪几张表中
- java常见面试题及答案
- 微信分享
- TX2 系统镜像 安装 caffe+tensorflow1.2.1
- 560. Subarray Sum Equals K