Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <algorithm>
- #include <ctime>
- #include <cctype>
- #include <cstdlib>
- #include <cassert>
- #include <functional>
- #include <iomanip>
- #include <string>
- #include <cstring>
- #include <map>
- #include <set>
- #include <vector>
- #include <queue>
- using namespace std;
- string bad = "kslrhgigbrktbtgbtbrt";
- int s, f;
- vector <pair <int, int> > g[2000];
- queue < pair<int, int> > bfs;
- int used[2000];
- int ans = 1e9;
- int m[2000][2000];
- string sum(string a, string b)
- {
- int flag = 0;
- string s1 = "";
- while(a.size() != b.size())
- {
- if(a.size() > b.size())
- b = '0' + b;
- else
- a = '0' + a;
- }
- for(int i = 0; i <= a.size(); i++)
- s1 += '0';
- for(int i = max(a.size(), b.size()) - 1; i >= 0; i--)
- {
- int a1, b1;
- a1 = int(a[i]) - int('0');
- b1 = int(b[i]) - int('0');
- s1[i + 1] = char( ((a1 + b1 + flag) % 10) + '0' );
- //cerr << a1 << " " << b1 << " " << s1[i + 1] << endl;
- flag = (a1 + b1 + flag) / 10;
- }
- s1[0] = char(flag + '0');
- //cerr << a << "+" << b << "=" << s1 << endl;
- return s1;
- }
- string rec(int x)
- {
- cerr << "!" << x << " " << used[x] << endl;
- if(x == s)
- {
- return("1");
- }
- string res = "0000";
- for(int i = 0; i < g[x].size(); i++)
- {
- cerr << g[x][i].first << " next - " << used[g[x][i].first] << " cost - " << g[x][i].second << endl;
- if(used[g[x][i].first] == used[x] - g[x][i].second)
- {
- res = sum(res, rec(g[x][i].first));
- }
- }
- return res;
- }
- int main ()
- {
- int n, m;
- cin >> n >> m;
- for(int i = 0; i < 2000; i++)
- used[i] = 239;
- for(int i = 0; i < m; i++)
- {
- int a, b, c;
- scanf("%d%d%d", &a, &b, &c);
- g[a].push_back(make_pair(b, c));
- g[b].push_back(make_pair(a, c));
- }
- cin >> s >> f;
- bfs.push( make_pair(s, 0) );
- used[s] = 0;
- while(!bfs.empty())
- {
- pair<int, int> t;
- t = bfs.front();
- if(t.first == f)
- {
- ans = min(ans, t.second);
- }
- bfs.pop();
- for(int i = 0; i < g[t.first].size(); i++)
- {
- if(used[ g[t.first][i].first ] == 239)
- {
- bfs.push(make_pair(g[t.first][i].first, g[t.first][i].second + t.second));
- used[g[t.first][i].first] = g[t.first][i].second + t.second;
- }
- }
- }
- //for(int i = 0; i < 2000; i++)
- // used[i] = 0;
- if(ans == 1e9)
- {
- cout << 0;
- return 0;
- }
- string answer = rec(f);
- cerr << answer;
- int j = 0;
- while(answer[j] == '0')
- {
- j++;
- }
- for(int i = j; i < answer.size(); i++)
- cout << answer[i];
- return 0;
- }
Add Comment
Please, Sign In to add comment