kuangbin专题专题四 MPI Maelstrom POJ - 1502
题目链接:https://vjudge.net/problem/POJ-1502
dijkstra板子题,题目提供下三角情况,不包含正对角线,因为有题意都为0,处理好输入,就是一个很水的题。
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 #include <string> 6 #include <vector> 7 using namespace std; 8 9 typedef long long LL; 10 #define inf (1LL << 30) - 1 11 #define rep(i,j,k) for(int i = (j); i <= (k); i++) 12 #define rep__(i,j,k) for(int i = (j); i < (k); i++) 13 #define per(i,j,k) for(int i = (j); i >= (k); i--) 14 #define per__(i,j,k) for(int i = (j); i > (k); i--) 15 16 const int N = 110; 17 vector<pair<int,int> > G[N]; 18 int val[N]; 19 int vis[N]; 20 int n; 21 22 int fun(string& x){ 23 int sum = 0; 24 25 rep__(i,0,(int)x.length()) sum = sum * 10 + (x[i] - '0'); 26 27 return sum; 28 } 29 30 void input(){ 31 32 string w; 33 rep(i,2,n){ 34 rep__(j,1,i){ 35 cin >> w; 36 37 if(w[0] == 'x') continue; 38 int W = fun(w); 39 G[i].push_back(make_pair(j,W)); 40 G[j].push_back(make_pair(i,W)); 41 } 42 } 43 } 44 45 int dijkstra(){ 46 47 if(n == 1) return 0; 48 val[1] = 0; 49 rep(i,2,n) val[i] = inf; 50 51 rep__(i,0,(int)G[1].size()) val[G[1][i].first] = G[1][i].second; 52 vis[1] = true; 53 54 rep(i,2,n){ 55 56 int x = -1; 57 int v = inf; 58 59 rep(j,1,n){ 60 if(!vis[j] && v > val[j]) v = val[x = j]; 61 } 62 63 if(x == -1) continue; 64 vis[x] = true; 65 66 rep__(o,0,(int)G[x].size()){ 67 int v = G[x][o].first; 68 int w = G[x][o].second; 69 if(!vis[v] && val[v] > val[x] + w) 70 val[v] = val[x] + w; 71 } 72 } 73 74 int ans = 0; 75 rep(i,2,n) ans = max(ans, val[i]); 76 77 return ans; 78 } 79 80 int main(){ 81 82 ios::sync_with_stdio(false); 83 cin.tie(0); 84 85 cin >> n; 86 input(); 87 cout << dijkstra() << endl; 88 89 getchar();getchar(); 90 return 0; 91 }
1