标签:AC 双向 tle ret vector 正整数 == 畅通工程 color
1 #include<iostream> 2 #include<queue> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 #include<vector> 7 const int maxn = 205; 8 vector <pair<int, int> > E[maxn]; 9 int dis[maxn]; 10 int n, m,s,t; 11 void init() 12 { 13 for (int i = 0; i < maxn; i++) 14 E[i].clear(), dis[i] = 1e9; 15 } 16 void dit() 17 { 18 dis[s] = 0; 19 priority_queue<pair<int, int> > q; 20 q.push(make_pair(-dis[s], s)); 21 while (!q.empty()) 22 { 23 int te = q.top().second; 24 q.pop(); 25 for (int i = 0; i < E[te].size(); i++) 26 { 27 int d = E[te][i].first; 28 int k = E[te][i].second; 29 if (dis[d] > dis[te] + k) 30 { 31 dis[d] = dis[te] + k; 32 q.push(make_pair(-dis[d], d)); 33 } 34 } 35 } 36 if (dis[t] == 1e9) 37 cout << "-1" << endl; 38 else cout << dis[t] << endl; 39 } 40 int main() 41 { 42 while (cin >> n >> m) 43 { 44 init(); 45 for (int i = 0; i < m; i++) 46 { 47 int t1, t2, t3; 48 scanf("%d %d %d", &t1, &t2, &t3); 49 E[t1].push_back(make_pair(t2, t3)); 50 E[t2].push_back(make_pair(t1, t3)); 51 } 52 scanf("%d %d", &s, &t); 53 dit(); 54 } 55 return 0; 56 }
标签:AC 双向 tle ret vector 正整数 == 畅通工程 color
原文地址:https://www.cnblogs.com/kangdong/p/9185114.html