题目链接:http://codeforces.com/gym/101061/problem/C
题意:一张图,图上的边有两种,一种是车道,一种是人行道。一个人要从A点到B点,可以坐车也可以走人行道。这个人希望在走最少的路的情况下尽可能早地到达B点(保证走路最少的清空下坐车时间最少),问要走多少路,一共花费多久。
pair<int, int>保存这个人需要走路的时间和共计的时间,读入更新图的时候需要判断仔细。利用pair自身比较运算符优先判断first元素可以直接跑floyd。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <cassert> 24 #include <cstdio> 25 #include <bitset> 26 #include <vector> 27 #include <deque> 28 #include <queue> 29 #include <stack> 30 #include <ctime> 31 #include <set> 32 #include <map> 33 #include <cmath> 34 //#include <unordered_map> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rll(a) scanf("%I64d", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onenum(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef pair<LL, LL> pll; 65 typedef map<string, int> msi; 66 typedef vector<int> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70 71 pii operator+(pii A, pii B) { 72 return pii(A.first + B.first, A.second + B.second); 73 } 74 const int maxn = 1010; 75 int n, m; 76 pii dp[maxn][maxn]; 77 78 int main() { 79 // FRead(); 80 int T; 81 int x, y, c, k; 82 Rint(T); 83 W(T) { 84 Rint(n); Rint(m); 85 Rep(i, n+5) { 86 Rep(j, n+5) dp[i][j] = pii(9000000, 9000000); 87 dp[i][i] = pii(0, 0); 88 } 89 Rep(i, m) { 90 Rint(x); Rint(y); Rint(c); Rint(k); 91 if(k == 1) { 92 if(dp[x][y].first > c) { 93 dp[x][y].first = min(dp[x][y].first, c); 94 dp[y][x].first = min(dp[y][x].first, c); 95 dp[x][y].second = min(dp[x][y].second, c); 96 dp[y][x].second = min(dp[y][x].second, c); 97 } 98 } 99 else { 100 if(dp[x][y].first != 0) { 101 dp[x][y].first = dp[y][x].first = 0; 102 dp[x][y].second = dp[y][x].second = c; 103 } 104 else { 105 dp[x][y].second = min(dp[x][y].second, c); 106 dp[y][x].second = min(dp[y][x].second, c); 107 } 108 } 109 } 110 For(k, 1, n+1) For(i, 1, n+1) For(j, 1, n+1) 111 dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); 112 Rint(x); Rint(y); 113 if(dp[x][y].second >= 9000000) puts("-1"); 114 else printf("%d %d\n", dp[x][y].first, dp[x][y].second); 115 } 116 RT 0; 117 }
时间: 2024-12-18 00:02:35