题目链接:51nod 1459 迷宫游戏
dij裸题。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #include<queue> 6 #define CLR(a,b) memset((a),(b),sizeof((a))) 7 using namespace std; 8 const int inf = 0x3f3f3f3f; 9 const int N = 500; 10 int d[N], vis[N]; 11 int a[N];//存每个房间的得分 12 int ans[N]; //记录所经过的点的权值和 13 int n, m, st, ed; 14 struct qnode{ 15 int v, c; 16 qnode(int _v = 0,int _c = 0):v(_v),c(_c) {} 17 bool operator < (const qnode &r)const{ 18 return r.c < c; 19 } 20 }; 21 struct Edge{ 22 int v, w; 23 Edge(int _v = 0, int _w = 0):v(_v),w(_w) {} 24 }; 25 vector<Edge>E[N]; 26 void addedge(int u, int v, int w){ 27 E[u].push_back(Edge(v, w)); 28 } 29 void dij(){ 30 priority_queue<qnode>q; 31 for(int i = 0; i < n; ++i){ 32 d[i] = inf; 33 vis[i] = 0; 34 ans[i] = 0; 35 } 36 d[st] = 0; 37 ans[st] = a[st]; 38 q.push(qnode(st, 0)); 39 while(!q.empty()){ 40 qnode t = q.top(); q.pop(); 41 int u = t.v; 42 if(vis[u]) 43 continue; 44 vis[u] = 1; 45 for(int i = 0; i < E[u].size(); ++i){ 46 int v = E[u][i].v; 47 int w = E[u][i].w; 48 if(!vis[v] && d[u] + w < d[v]){ 49 d[v] = d[u] + w; 50 ans[v] = ans[u] + a[v]; 51 q.push(qnode(v, d[v])); 52 } 53 else if(!vis[v] && d[u] + w == d[v]){ 54 ans[v] = max(ans[v], ans[u] + a[v]); 55 } 56 } 57 58 } 59 } 60 int main(){ 61 int x, y, z; 62 scanf("%d%d%d%d", &n, &m, &st, &ed); 63 for(int i = 0; i < n; ++i) 64 scanf("%d", &a[i]); 65 while(m--){ 66 scanf("%d%d%d", &x, &y, &z); 67 addedge(x, y, z); 68 addedge(y, x, z); 69 } 70 dij(); 71 printf("%d %d ", d[ed], ans[ed]); 72 return 0; 73 }