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
#include <stdlib.h> #include <cstdio> #include <string> #include <string.h> #include <iostream> #include <queue> #include <cmath> #include <algorithm> #include <set> #include <vector> #define N 1000 using namespace std; typedef long long ll; struct node{ int to,v,Next; }; node side[250005]; //坑点 int len; int head[10005]; void add(int x,int y,int v) { side[len].to = y; side[len].v = v; side[len].Next = head[x]; head[x] = len++; } int have[1005]; int dis[1005]; int wz[1005]; int mk[1005]; int tim[1005]; int path[1005]; struct Node{ int d,w,id; bool operator < (const Node b) const { if(d != b.d) return d > b.d; return w < b.w; } }; void dijstrea(int s,int d) { memset(dis,63,sizeof(dis)); memset(wz,0,sizeof(wz)); memset(mk,0,sizeof(mk)); memset(tim,0,sizeof(tim)); tim[s] = 1; wz[s] = have[s]; dis[s] = 0; path[s] = -1; priority_queue <Node> pq; Node tmp; tmp.id = s; tmp.d = 0; tmp.w = have[s]; pq.push(tmp); while(pq.size()) { tmp = pq.top(); pq.pop(); if(tmp.id == d) break; if(mk[tmp.id]) continue; mk[tmp.id] = 1; for(int i = head[tmp.id]; i!= -1; i =side[i].Next) { int y = side[i].to; int v = side[i].v; int w = have[y]; if(dis[y] > dis[tmp.id] + v) { path[y] = tmp.id; dis[y] = dis[tmp.id] + v; tim[y] = tim[tmp.id]; //坑点 wz[y] = wz[tmp.id] + have[y]; Node nd; nd.d = dis[y]; nd.id = y; nd.w = wz[y]; pq.push(nd); } else if(dis[y] == dis[tmp.id] + v) { tim[y] += tim[tmp.id]; if(tmp.w + w > wz[y]) { path[y] = tmp.id; wz[y] = tmp.w + have[y]; Node nd; nd.d = dis[y]; nd.id = y; nd.w = wz[y]; pq.push(nd); } } } } } int f; void print(int x) { if(x == -1) return; print(path[x]); printf(f==0 ? "%d" : " %d",x); f = 1; } int res[1000]; int main() { int n,m,s,d; cin >> n >> m >>s >> d; for(int i = 0; i < n; i++) { scanf("%d",&have[i]); } memset(path,-1,sizeof(path)); memset(head,-1,sizeof(head)); len = 0; for(int i = 1; i <= m; i++) { int x,y,d; scanf("%d%d%d",&x,&y,&d); add(x,y,d); add(y,x,d); } dijstrea(s,d); printf("%d %d\n",tim[d],wz[d]); print(d); return 0; }