pat 1003. Emergency (25)

来源:互联网 发布:游戏人物模型制作软件 编辑:程序博客网 时间:2024/06/05 11:55

https://www.patest.cn/contests/pat-a-practise/1003

#include <cstdio>#include <vector>#include <climits>using namespace std;pair<int,int> dijstra(vector<vector<int>> g, vector<int> hands,int c1,int c2,int n) {vector<bool> v(n, 0);vector<int> dist(n, INT_MAX);vector<int> chands(n,0);vector<int> times(n,0);dist[c1] = 0;times[c1] = 1;chands[c1] = hands[c1];for (int i = 0; i < n; i++){int tnode = c1, tdist = INT_MAX, thands=0;for (int j = 0; j < n; j++){if (!v[j] &&(dist[j] < tdist ||(dist[j] == tdist && chands[j]>thands))) {tnode = j;tdist = dist[j];thands = chands[j];}}//printf("tnode: %d %d %d\n",tnode, tdist,thands);v[tnode] = true;if (tnode == c2) return make_pair(times[c2],chands[c2]);for (int j = 0; j < n; j++){if (!v[j]&& g[tnode][j] != INT_MAX) {if (dist[j] > dist[tnode] + g[tnode][j]) {dist[j] = dist[tnode] + g[tnode][j];times[j] = times[tnode];chands[j] = chands[tnode] + hands[j];}else if(dist[j] == dist[tnode] + g[tnode][j]) {times[j] = times[j] + times[tnode];if (chands[j] < chands[tnode] + hands[j]) {chands[j] = chands[tnode] + hands[j];}}}}}}int main(){int n, m, c1, c2, a, b, d;scanf("%d %d %d %d",&n,&m,&c1,&c2);vector< vector<int> > g(n, vector<int>(n, INT_MAX));vector<int> hands(n);for (int i = 0; i < n; i++){scanf("%d",&hands[i]);}for (int i = 0; i < m; i++){scanf("%d %d %d",&a,&b,&d);g[a][b] = g[b][a] = d;}pair<int,int> times_hands = dijstra(g,hands,c1,c2,n);printf("%d %d\n",times_hands.first,times_hands.second);    return 0;}


0 0
原创粉丝点击