text stringlengths 49 983k |
|---|
#include<cstdio>
#include<functional>
#include<algorithm>
using namespace std;
int main(void)
{
int y1,m1,d1,y2,m2,d2,x;
scanf("%d %d %d",&y1,&m1,&d1);
scanf("%d %d %d",&y2,&m2,&d2);
if(y1>y2 || (y1==y2 && m1>m2) || (y1==y2 && m1==m2 && d1>d2)) {
swap(y1,y2); swap(m1,m2); swap(d1,d2);
}
x=y2-y1;
if(m2>m1 || (m2==m1 && d2>d1)) x++;
printf("%d\n",x);
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int,int>P;
struct st{int a,b,c;};
bool operator<(st&a,st&b){
if(a.a!=b.a)return a.a<b.a;
if(a.b!=b.b)return a.b<b.b;
if(a.c!=b.c)return a.c<b.c;
return false;
}
bool operator==(st&a,st&b){
return a.a==b.a&&a.b==b.b&&a.c==b.c;
}
int main(){
st a,b;cin>>a.a>>a.b>>a.c>>b.a>>b.b>>b.c;
if(a<b)swap(a,b);
if(P(a.b,a.c)<=P(b.b,b.c))cout<<a.a-b.a<<endl;
else cout<<a.a-b.a+1<<endl;
} |
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> V;
typedef pair<ll,ll> P;
typedef map<ll,ll> M;
#define REP(i,n) for(ll (i) = 0; (i) < (n); ++(i))
#define FOR(i,j,n) for(ll (i) = (j); (i) < (n); ++(i))
#define REPR(i,n) for(ll (i) = (n); (i) >= 0; --i))
signed main(){
int y1,m1,d1,y2,m2,d2;
cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
--m1;--d1;--m2;--d2;
V turn = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
V turn2 = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ans = 0;
int age1 = 3000 - y1;
int age2 = 3000 - y2;
REP(i,12){
REP(j, turn[i]){
ans = max(ans, abs(age1 - age2));
if((m1 == i && d1 == j) || (m1 == 2 && d1 == 29 && i == 3 && j == 1))
++age1;
if((m2 == i && d2 == j) || (m2 == 2 && d2 == 29 && i == 3 && j == 1))
++age2;
ans = max(ans, abs(age1 - age2));
}
}
age1 = 3000 - y1;
age2 = 3000 - y2;
REP(i,12){
REP(j, turn2[i]){
ans = max(ans, abs(age1 - age2));
if(m1 == i && d1 == j)
++age1;
if(m2 == i && d2 == j)
++age2;
ans = max(ans, abs(age1 - age2));
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
int main(){
vector<int>a(3),b(3);
cin>>a[0]>>a[1]>>a[2]>>b[0]>>b[1]>>b[2];
if(a<b)swap(a,b);
if(make_pair(a[1],a[2])<=make_pair(b[1],b[2]))cout<<a[0]-b[0]<<endl;
else cout<<a[0]-b[0]+1<<endl;
} |
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
int main(){
vector<int>a(3),b(3);
cin>>a[0]>>a[1]>>a[2]>>b[0]>>b[1]>>b[2];
if(a<b)swap(a,b);
if(vector<int>{a[1],a[2]}<=vector<int>{b[1],b[2]})cout<<a[0]-b[0]<<endl;
else cout<<a[0]-b[0]+1<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(false), cout << fixed << setprecision(20);
#define int long long
#define gcd __gcd
#define all(x) (x).begin(), (x).end()
template<class T>
bool chmax(T& a, T b){return (a = max(a, b)) == b;}
template<class T>
bool chmin(T& a, T b){return (a = min(a, b)) == b;}
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) _overload(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
#define _rev(i, n) revi(i, n, 0)
#define revi(i, a, b) for(int i = (int)(a - 1); i >= (int)(b); i--)
#define rev(...) _overload(__VA_ARGS__, revi, _rev)(__VA_ARGS__)
#define each(i, n) for(auto&& i: n)
const int INF = 1e18, MOD = 1e9 + 7;
signed main() {
INCANT;
int y, m, d, Y, M, D, res = 0;
cin>>y>>m>>d>>Y>>M>>D;
if(y == Y){
if(m == M && d == D){
cout<<0<<endl;
}else{
cout<<1<<endl;
}
}else if(y > Y){
if(m == M){
if(d == D){
cout<<y - Y<<endl;
}else if(d > D){
cout<<y - Y + 1<<endl;
}else{
cout<<y - Y<<endl;
}
}else if(m > M){
cout<<y - Y + 1<<endl;
}else{
cout<<y - Y<<endl;
}
}else{
if(m == M){
if(d == D){
cout<<Y - y<<endl;
}else if(d > D){
cout<<Y - y<<endl;
}else{
cout<<Y - y<<endl;
}
}else if(m > M){
cout<<Y - y<<endl;
}else{
cout<<Y - y + 1<<endl;
}
}
}
|
#include <iostream>
using namespace std;
int main() {
int y1, m1, d1; cin >> y1 >> m1 >> d1;
int y2, m2, d2; cin >> y2 >> m2 >> d2;
if (y1 * 10000 + m1 * 100 + d1 > y2 * 10000 + m2 * 100 + d2) {
y1 ^= y2 ^= y1 ^= y2;
m1 ^= m2 ^= m1 ^= m2;
d1 ^= d2 ^= d1 ^= d2;
}
int ans = y2 - y1;
if (m1 * 100 + d1 < m2 * 100 + d2) ans++;
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define V vector
#define VI vector<int>
#define VVI vector<vector<int>>
#define rep(i,n) for(int i=0;i<(n);i++)
#define MOD 1000000007
#define S second
#define F first
using namespace std;
int main(void){
pair<int, pair<int, int> > y1, y2;
cin >> y1.F >> y1.S.F >> y1.S.S;
cin >> y2.F >> y2.S.F >> y2.S.S;
if ( y1 > y2 ) swap(y1, y2);
int pp=0;
if(y1.S<y2.S)pp=1;
cout << y2.F - y1.F + pp<< endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;
int main() {
pair< int, pair< int, int > > a, b;
cin >> a.first >> a.second.first >> a.second.second;
cin >> b.first >> b.second.first >> b.second.second;
if (a > b) swap(a, b);
if (a.second < b.second) cout << b.first-a.first+1 << endl;
else cout << b.first-a.first << endl;
return 0;
}
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)
#define seg_size 524288
int main(){
vector<tuple<int, int, int>> go;
REP(i, 2) {
int a, b, c;
cin >> a >> b >> c;
go.push_back(make_tuple(a, b, c));
}
sort(go.begin(), go.end());
int ans = get<0>(go[1]) - get<0>(go[0]);
if (get<1>(go[1]) > get<1>(go[0])) {
ans++;
}
else if (get<1>(go[1]) == get<1>(go[0])) {
if (get<2>(go[1]) > get<2>(go[0])) {
ans++;
}
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int y[2], m[2], d[2];
int i;
for (i = 0; i < 2; i++) {
cin >> y[i] >> m[i] >> d[i];
}
int result = max(y[0], y[1]) - min(y[0], y[1]) + 1;
if (y[0] > y[1]) {
if (m[0] < m[1]) {
result--;
} else if (m[0] == m[1] && d[0] < d[1]) {
result--;
}
} else if (y[0] < y[1]) {
if (m[0] > m[1]) {
result--;
} else if (m[0] == m[1] && d[0] > d[1]) {
result--;
}
} else if (m[0] == m[1] && d[0] == d[1]) {
result--;
}
cout << result << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
const int32_t inf = 1001001001;
const int64_t infll = 1001001001001001001ll;
const int dx[] = {0, -1, 1, 0, -1, 1, -1, 1}, dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};
template <typename T> using vector2d = vector<vector<T>>;
// clang-format off
template <typename T> void sort(vector<T> &v) { sort(all(v)); }
// ostream &operator<<(ostream &os, __int128_t value) { if (ostream::sentry(os)) { __uint128_t tmp = value < 0 ? -value : value; char buffer[64]; char *d = end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; }
// istream &operator>>(istream &is, __int128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } if (in[0] == '-') value *= -1; return is; }
// ostream &operator<<(ostream &os, __uint128_t value) { if (ostream::sentry(os)) { char buffer[64]; char *d = end(buffer); do { --d; *d = "0123456789"[value % 10]; value /= 10; } while (value != 0); int len = end(buffer) - d; if (os.rdbuf()->sputn(d, len) != len) { os.setstate(ios_base::badbit); }} return os; }
// istream &operator>>(istream &is, __uint128_t &value) { string in; is >> in; value = 0; for (const char &c : in) { if ('0' <= c && c <= '9') value = 10 * value + (c - '0'); } return is; }
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { os << v[0]; for (int i = 1; i < v.size(); ++i) os << " " << v[i]; return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << p.fs << " " << p.sc; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.fs >> p.sc; return is; }
struct IoSetup { IoSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup;
inline int64_t in() { int64_t x = 0; cin >> x; return x; }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// clang-format on
struct date {
int y, m, d;
date() {}
date(int y, int m, int d) : y(y), m(m), d(d) {}
bool operator<(date a) {
if (y == a.y) {
if (m == a.m) {
return d < a.d;
}
return m < a.m;
}
return y < a.y;
}
};
bool izryt(date &a, date &b) {
if (a.m == b.m) {
return a.d < b.d;
}
return a.m < b.m;
}
istream &operator>>(istream &is, date &a) {
is >> a.y >> a.m >> a.d;
return is;
}
signed main(int argc, char *argv[]) {
date a, b;
cin >> a >> b;
if (b < a) swap(a, b);
cout << b.y - a.y + izryt(a, b) << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
signed main(){
vector<int>a(3),b(3);
rep(i,3)cin>>a[i];
rep(i,3)cin>>b[i];
if(a>b)swap(a,b);
int ans=b[0]-a[0];
b[0]=a[0];
if(a<b)ans++;
cout<<ans<<endl;
return 0;
} |
//h17-3
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define segment_size 65536
#define ROOP() while (true)
int main(){
int y1,m1,d1,y2,m2,d2;
cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
if(y1*10000+m1*100+d1 > y2*10000+m2*100+d2){
swap(y1,y2);
swap(m1,m2);
swap(d1,d2);
}
int ans = y2 - y1;
if(m1*100+d1 < m2*100+d2) ans++;
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int y1,m1,d1;
int y2,m2,d2;
int SA;
cin>>y1>>m1>>d1;
cin>>y2>>m2>>d2;
SA=0;
if(y1>y2){
if(m1>m2) SA=y1-y2+1;
else if(m1==m2){
if(d1>d2) SA=y1-y2+1;
else SA=y1-y2;
}
else SA=y1-y2;
}
else if(y1<y2){
if(m1<m2) SA=y2-y1+1;
else if(m1==m2){
if(d1<d2) SA=y2-y1+1;
else SA=y2-y1;
}
else SA=y2-y1;
}
else{
if(m1!=m2||d1!=d2) SA=1;
}
cout<<SA<<endl;
return 0;
}
|
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
struct date {
int by, bm, bd;
int y, m, d;
int days[13] = {0,
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
date(int by, int bm, int bd, int y, int m, int d):
by(by), bm(bm), bd(bd), y(y), m(m), d(d)
{}
bool is_uruu(int y) {
if (y % 400 == 0) return true;
if (y % 100 == 0) return false;
if (y % 4 == 0) return true;
return false;
}
void increment_date() {
int maxd = days[m];
if (is_uruu(y) && m == 2) ++maxd;
if (maxd == d++) {
d = 1;
if (m++ == 12) {
m = 1;
++y;
}
}
}
int get() const {
int res = y-by;
if (!(bm < m || (bm == m && bd <= d))) --res;
return res;
}
};
int main() {
int y0, m0, d0;
scanf("%d %d %d", &y0, &m0, &d0);
int y1, m1, d1;
scanf("%d %d %d", &y1, &m1, &d1);
int cy, cm, cd;
std::tie(cy, cm, cd) = std::max(std::make_tuple(y0, m0, d0),
std::make_tuple(y1, m1, d1));
date a0(y0, m0, d0, cy, cm, cd);
date a1(y1, m1, d1, cy, cm, cd);
int res = 0;
for (int i = 0; i < 366*410; ++i) {
int x = a0.get();
int y = a1.get();
int cur = abs(x-y);
res = std::max(res, cur);
a0.increment_date();
a1.increment_date();
}
printf("%d\n", res);
}
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct s_ymd {
int y, m, d;
bool operator!=(const s_ymd& s){
return y != s.y || m != s.m || d != s.d;
}
};
int main() {
vector<s_ymd> v(2); int ans = 0;
auto f_sort = [](const s_ymd& l, const s_ymd& r) {
return l.y == r.y ? l.m == r.m ? l.d < r.d : l.m < r.m : l.y < r.y;
};
for (int i = 0; i < 2; i++) cin >> v[i].y >> v[i].m >> v[i].d;
if (v[0] != v[1]) sort(v.begin(), v.end(), f_sort);
ans += v[1].y - v[0].y;
if (v[0].m < v[1].m) ans++;
else if (v[0].m == v[1].m && v[0].d < v[1].d) ans++;
cout << ans << endl;
return 0;
}
|
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f;
b = b * 100 + c;
e = e * 100 + f;
if (d > a) {
int ans = d - a;
if (e > b)ans++;
cout << ans << endl;
}
else if (d < a) {
int ans = a - d;
if (e < b)ans++;
cout << ans << endl;
}
else if (d == a) {
if (b == e)cout << 0 << endl;
else cout << 1 << endl;
}
}
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(){
int b[2][3];
for(int i = 0;i < 2;i++){
for(int j = 0;j < 3;j++){
cin >> b[i][j];
}
}
if(b[0][0] == b[1][0]){
int d1 = 100 * b[0][1] + b[0][2],d2 = 100 * b[1][1] + b[1][2];
if(d1 == d2){
cout << 0 << endl;
}else{
cout << 1 << endl;
}
return 0;
}else if(b[0][0] > b[1][0]){
for(int i = 0;i < 3;i++){
swap(b[0][i],b[1][i]);
/*
int tmp;
tmp = b[0][i];
b[0][i] = b[1][i];
b[1][i] = tmp;
*/
}
}
int ans = b[1][0] - b[0][0];
int d1 = 100 * b[0][1] + b[0][2],d2 = 100 * b[1][1] + b[1][2];
if(d2 - d1 > 0)ans++;
cout << ans << endl;
}
|
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
# include <unordered_map>
# include <numeric>
# include <complex>
# include <bitset>
# include <random>
# include <chrono>
# include <cstdlib>
# include <tuple>
# include <array>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using D = double;
constexpr int INF = 2147483647;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr double HDINF = 50000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_num(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq) (qpqpq).begin(),(qpqpq).end()
# define RALL(qpqpq) (qpqpq).rbegin(),(qpqpq).rend()
# define UNIQUE(wpwpw) sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe) transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr) transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy) for(LL i=(tptpt);i<(ypypy);i++)
# define RFOR(i,tptpt,ypypy) for(LL i=(tptpt);i>=(ypypy);i--)
# define REP(i,upupu) FOR(i,0,upupu)
# define INIT std::ios::sync_with_stdio(false);std::cin.tie(0)
int a, b, c, d, e, f;
int main() {
cin >> a >> b >> c >> d >> e >> f;
c += b * 100;
f += e * 100;
if (a<d) cout << (f>c ? d - a + 1 : d - a) << endl;
else if (a>d) cout << (f<c ? a - d + 1 : a - d) << endl;
else cout << (f == c ? 0 : 1) << endl;
}
|
//yukicoder@cpp14
//author:luckYrat(twitter:@luckYrat_)
//<ここに一言>
//せんげん!
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <cctype>
#include <utility>
#include <climits>
//なまえくーかん!
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
//てーすう!
const int mod = 1000000007;
const int inf = (1<<30)-1;
const ll linf = (1LL<<62LL)-1;
const double EPS = (1e-10);
//でふぁいん!
#define anyfill(n,s) setw(n) << setfill(s)
#define loop(s) for(int i = 0; s > i; i++)
#define rep(i,q) for(int i = 0; (q) > i; i++)
#define repp(i,n,q) for(int i = n; (q) > i; i++)
#define dep(i,q) for(int i = (q); 0 < i; i--)
//みじかく!
#define pb push_back
#define fir first
#define scn second
#define ednl endl
//いぇすのー!
#define YesNo(a) (a?"Yes":"No")
#define YESNO(a) (a?"YES":"NO")
#define yesno(a) (a?"yes":"no")
//きんぼーnほーこー!!
P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる
計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/
template <typename T>
T gcd(T a,T b){
if(a%b==0)return b;
else return gcd(b,a%b);
}
template <typename T>
T lcm(T a,T b){
return a/gcd(a,b)*b;
}
struct z{
int y,m,d;
};
int cmp(const z &a,const z &b){
if(a.y==b.y){
if(a.m==b.m){
if(a.d==b.d){
return 0;
}else if(a.d < b.d){
return 1;
}else{
return -1;
}
}else if(a.m < b.m){
return 1;
}else{
return -1;
}
}else if(a.y < b.y){
return 1;
}else{
return -1;
}
}
int cmpmd(const z &a,const z &b){
if(a.m==b.m){
if(a.d==b.d){
return 0;
}else if(a.d < b.d){
return 1;
}else{
return -1;
}
}else if(a.m < b.m){
return 1;
}else{
return -1;
}
}
__attribute__((constructor))
void initial() {
cin.tie(0);
ios::sync_with_stdio(false);
}
int main(){
z A,B;
cin>>A.y>>A.m>>A.d;
cin>>B.y>>B.m>>B.d;
if(cmp(A,B)==-1){
swap(A,B);
}
if(!cmpmd(A,B)){
cout << B.y-A.y;
}else if(cmpmd(A,B)==-1){
cout << B.y-A.y;
}else{
cout << B.y-A.y+1;
}
cout << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
bool cmp(int m1,int d1,int m2,int d2){
if(m1>m2){
return true;
}
if(m1==m2){
if(d1>=d2){
return true;
}
}
return false;
}
int main(){
int y1,y2;
int m1,m2;
int d1,d2;
cin>>y1>>m1>>d1;
cin>>y2>>m2>>d2;
int ans=abs(y1-y2)+1;
if(y1==y2){
if(m1==m2&&d1==d2)ans--; //!!!!!!
}
if(y1<y2){
if(cmp(m1,d1,m2,d2)){
ans--;
}
}
if(y1>y2){
if(cmp(m2,d2,m1,d1)){
ans--;
}
}
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int,int>P;
struct st{int a,b,c;};
bool operator<(st&a,st&b){
if(a.a!=b.a)return a.a<b.a;
if(a.b!=b.b)return a.b<b.b;
if(a.c!=b.c)return a.c<b.c;
return false;
}
bool operator==(st&a,st&b){
return a.a==b.a&&a.b==b.b&&a.c==b.c;
}
int main(){
st a,b;cin>>a.a>>a.b>>a.c>>b.a>>b.b>>b.c;
if(a<b)swap(a,b);//a??????????????\????????????????????????
if(a==b){puts("0");return 0;}
if(P(a.b,a.c)<=P(b.b,b.c))cout<<a.a-b.a<<endl;
else cout<<a.a-b.a+1<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
int main(){
int a,b,c,y,m,d;
cin>>a>>b>>c>>y>>m>>d;
P h=P(b,c),t=P(m,d);
int f=0;
if(a>y&&h>t)f=1;
if(a<y&&h<t)f=1;
if(a==y&&h!=t)f=1;
cout<<abs(a-y)+f<<endl;
return 0;
}
|
#include <cstdio>
#include <algorithm>
using namespace std;
int main(void){
int y1, m1, d1, y2, m2, d2, diff;
scanf("%d %d %d", &y1, &m1, &d1);
scanf("%d %d %d", &y2, &m2, &d2);
if (y2 < y1) {
swap(y1, y2); swap(m1, m2); swap(d1, d2);
}
diff = y2 - y1;
if (diff == 0) {
if (m1 == m2 && d1 == d2) printf("%d\n", diff);
else {
printf("%d\n", diff+1);
}
} else {
if (m1 == m2 && d1 == d2) printf("%d\n", diff);
else if (m1 == m2 && d1 != d2) {
if (d2 < d1) printf("%d\n", diff);
else printf("%d\n", diff+1);
}
else if (m1 != m2) {
if (m2 < m1) printf("%d\n", diff);
else printf("%d\n", diff+1);
}
}
return 0;
}
|
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
int y1, m1, d1, y2, m2, d2;
cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
if (y1 == y2) {
if (m2 < m1) {
swap(m2, m1);
swap(d1, d2);
}
if (m2 == m1) {
if (d2 < d1)swap(d2, d1);
}
}
if (y1 > y2) {
swap(y1, y2);
swap(m2, m1);
swap(d1, d2);
}
int ans = 0, ageA = 0, ageB = 0;
eFOR(i, y1, y2 + 6) {
eFOR(j, 1, 12) {
int k;
if (j == 2) {
k = 28;
if (i % 4 == 0 && i % 100 != 0)k++;
else if (i % 400 == 0)k++;
}
if (j == 4 || j == 6 || j == 9 || j == 11)k = 30;
else k = 31;
eFOR(d, 1, k) {
if (j == m1 && d == d1 && i>=y1) {
ageA++;
}
if (j == m2 && d == d2 && i>=y2) {
ageB++;
}
if (m1 == 2 && d1 == 29 && k == 28 && j == 3 && d == 1 && i >= y1) {
ageA++;
}
if (m2 == 2 && d2 == 29 && k != 29 && j == 3 && d == 1 && i >= y2)ageB++;
ans = max(ans, abs(ageA - ageB));
}
}
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
int main()
{
unsigned y1, m1, d1, y2, m2, d2;
std::cin >> y1 >> m1 >> d1;
std::cin >> y2 >> m2 >> d2;
if(y1 > y2 || (y1 == y2 && m1 > m2 || (m1 == m2 && d1 > d2)))
{
std::swap(y1, y2);
std::swap(m1, m2);
std::swap(d1, d2);
}
std::cout << (y2 - y1 + ((m2 > m1 || (m2 == m1 && d2 > d1)) ? 1 : 0))
<< '\n';
}
|
#include<bits/stdc++.h>
#define y first
#define s second
#define m second.first
#define d second.second
using namespace std;
typedef pair<int,int> p;
typedef pair<int,p> P;
int main(){
P a,b;
cin>>a.y>>a.m>>a.d>>b.y>>b.m>>b.d;
if(a>b)swap(a,b);
int ans=b.y-a.y;
if(a.s<b.s)ans++;
cout<<ans<<endl;
return 0;
} |
#include "bits/stdc++.h"
#define rep(i,a,n) for(int i = a;i < n;i++)
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
int main(){
int y[2],m[2],d[2],type = -1;
rep(i,0,2){
cin >> y[i] >> m[i] >> d[i];
}
if(m[0] > m[1]){
type = 1;
}
else if(m[0] < m[1]){
type = 2;
}
else{
if(d[0] > d[1]){
type = 1;
}
else if(d[0] < d[1]){
type = 2;
}
else{
type = 3;
}
}
switch(type){
case 1:
if(y[0] < y[1]){
cout << abs(y[0]-y[1]) << endl;
}
else{
cout << abs(y[0]-y[1])+1 << endl;
}
break;
case 2:
if(y[0] > y[1]){
cout << abs(y[0]-y[1]) << endl;
}
else{
cout << abs(y[0]-y[1])+1 << endl;
}
break;
case 3:
cout << abs(y[0]-y[1]) << endl;
break;
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int,int>P;
struct st{int a,b,c;};
bool operator<(st&a,st&b){
if(a.a!=b.a)return a.a<b.a;
if(a.b!=b.b)return a.b<b.b;
if(a.c!=b.c)return a.c<b.c;
return false;
}
int main(){
st a,b;cin>>a.a>>a.b>>a.c>>b.a>>b.b>>b.c;
if(a<b)swap(a,b);
if(P(a.b,a.c)<=P(b.b,b.c))cout<<a.a-b.a<<endl;
else cout<<a.a-b.a+1<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c,d,e,f;
cin>>a>>b>>c>>d>>e>>f;
cout<<abs(a-d)+(b!=e||c!=f)*( (a-d)*(b*100+c-e*100-f)>=0 )<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int y1, m1, d1, y2, m2, d2;
cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
if(y2 > y1 || y2 == y1 && m2 > m1 || y2 == y1 && m2 == m1 && d2 > d1){
swap(y1, y2);
swap(m1, m2);
swap(d1, d2);
}
int ans = y1 - y2 + (m1 > m2 || (m1 == m2 && d1 > d2) ? 1 : 0);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
typedef long long i64;
using std::cout;
using std::endl;
using std::cin;
int main() {
int a, b, c; cin >> a >> b >> c; std::pair<int, int> p = {b, c};
int A, B, C; cin >> A >> B >> C; std::pair<int, int> P = {B, C};
if(a > A) {
std::swap(a, A);
std::swap(p, P);
} else if(a == A) {
if(p.first > P.first) std::swap(p, P);
else if(p.first == P.first and p.second > P.second) std::swap(p, P);
}
if(P > p) cout << A - a + 1 << endl;
else cout << A - a << endl;
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
class day{
public:
int y,m,d;
int cnt = 0;
bool isU = false;
int f[12] = {0,0,0,1,0,1,0,0,1,0,1,0};
void next(){
isU = false;
if(y%4 == 0){
if(y%400 == 0) isU = true;
else if(y%100 == 0) isU = false;
else isU = true;
}
d++;
if(!isU && m == 2 && d == 29 ){ m++; d = 1;}
else if(isU && m == 2 && d == 30){m++; d = 1;}
else if(f[m-1] && d == 31){ m++; d = 1;}
else if(f[m-1] == 0 && d == 32){ m++; d = 1;}
if(m == 13){
m = 1;
y++;
cnt++;
}
}
bool isEq(int b, int c){
if(b == m && c == d) return true;
else return false;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int y[2],m[2],d[2];
bool tn[2];
for (int i = 0;i < 2;i++){
cin >> y[i] >> m[i] >> d[i];
tn[i] = false;
if(m[i] == 2 && d[i] == 29) tn[i] = true;
}
day x;
x.y = min(y[0],y[1]); x.m = 1; x.d = 0;
int a[] = {x.y-y[0],x.y-y[1]};
int ans = -1;
while(x.cnt != 4000){
x.next();
for (int i = 0;i < 2;i++){
if(x.isEq(m[i],d[i])){
a[i]++;
}
if(!x.isU && tn[i] && x.isEq(3,1)){
a[i]++;
}
}
int dis = abs(a[0]-a[1]);
ans = max(ans,dis);
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int y1, y2, m, d, x1, x2, u = 1;
cin >> y1 >> m >> d;
x1 = m * 100 + d;
cin >> y2 >> m >> d;
x2 = m * 100 + d;
if ((y1 - y2)*(x1 - x2) < 0 || x1 == x2)
u = 0;
cout << abs(y1 - y2) + u << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int days(int y,int m,int d){
if(m<3)y--,m+=12;
return 365*y+y/4-y/100+y/400+(153*m+8)/5+d;
}
void ymd(int day,int &y,int &m,int &d){
int lo=-1,hi=100000,mid;
while(lo+1<hi){
mid=(lo+hi)/2;
if(days(mid,12,31)<day)lo=mid;
else hi=mid;
}
y=hi;
for(m=1;m<12;m++)if(days(y,m+1,1)>day)break;
d=day-days(y,m,0);
}
int main() {
int y1,m1,d1,y2,m2,d2,a1=-1,a2=-1,ans=-1;
cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
int s=min(days(y1,m1,d1),days(y2,m2,d2));
for(int i=s; i<=s+3500*366; i++) {
int y,m,d;
ymd(i,y,m,d);
if(i>=days(y1,m1,d1)) {
if(m==m1&&d==d1) a1++;
else if(m1==2&&d1==29) {
int yy,mm,dd;
ymd(i-1,yy,mm,dd);
if(mm==2&&dd==28) a1++;
}
}
if(i>=days(y2,m2,d2)) {
if(m==m2&&d==d2) a2++;
else if(m2==2&&d2==29) {
int yy,mm,dd;
ymd(i-1,yy,mm,dd);
if(mm==2&&dd==28) a2++;
}
}
if(a1>=0&&a2>=0) ans=max(ans,abs(a1-a2));
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>
using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;
int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};
bool isuru(int y){
if(y % 400 == 0) return true;
else if(y % 100 == 0) return false;
else if(y % 4 == 0) return true;
else return false;
}
signed main(){
int y1, m1, d1; cin >> y1 >> m1 >> d1;
int y2, m2, d2; cin >> y2 >> m2 >> d2;
int cury = 1;
int curm = 1;
int curd = 1;
int age1 = 0;
int age2 = 0;
bool isuru1 = false;
bool isuru2 = false;
if(m1 == 2 and d1 == 29 and isuru(y1)) isuru1 = true;
if(m2 == 2 and d2 == 29 and isuru(y2)) isuru2 = true;
int ans = 0;
while(cury < 10000){
if(cury >= y1 and curm == m1 and curd == d1) age1++;
if(isuru1 and !isuru(cury) and cury >= y1 and curm == 3 and curd == 1) age1++;
if(cury >= y2 and curm == m2 and curd == d2) age2++;
if(isuru2 and !isuru(cury) and cury >= y2 and curm == 3 and curd == 1) age2++;
//cerr << cury << " " << curm << " " << curd << " " << age1 << " " << age2 << endl;
ans = max(ans, (int)abs(age1 - age2));
curd++;
if(!isuru(cury) and curm == 2 and curd == 29){
curd = 1;
curm++;
}else if(isuru(cury) and curm == 2 and curd == 30){
curd = 1;
curm++;
}else if((curm == 4 or curm == 6 or curm == 9 or curm == 11) and curd == 31){
curd = 1;
curm++;
}else if(curd == 32){
curd = 1;
curm++;
}
if(curm == 13){
curm = 1;
cury++;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;
int length_of_year(int y){ // 1-indexed
if(y%400 == 0 || ((y%100 != 0) && (y%4 == 0)))return 366;
return 365;
}
int length_of_month(int y, int m){ // 1-indexed
int monthes[12] = {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(m != 2)return monthes[m-1];
if(length_of_year(y) == 366)return 29;
return 28;
}
/*int date_to_int(string date){ //ex) 2018/08/05
int y = stoi(date.substr(0, 4)), m = stoi(date.substr(5, 2)), d = stoi(date.substr(8, 2));
int ans = 0;
repi(i,1,y)ans += length_of_year(i);
repi(i,1,m)ans += length_of_month(y, i);
return ans + d;
}*/
/*string int_to_date(int date){
int y = 1, m = 1;
for(int i = 1;length_of_year(i) < date;i++){date -= length_of_year(i);y++;}
for(int i = 1;length_of_month(y, i) < date;i++){date -= length_of_month(y, i);m++;}
string ans;
ans += string(4-to_string(y).size(), '0') + to_string(y) + "/";
ans += string(2-to_string(m).size(), '0') + to_string(m) + "/";
ans += string(2-to_string(date).size(), '0') + to_string(date);
return ans;
}*/
int struct_to_int(int y, int m, int d){
int ans = 0;
repi(i,1,y)ans += length_of_year(i);
repi(i,1,m)ans += length_of_month(y, i);
return ans+d;
}
/*string struct_to_date(int y, int m, int d){
return int_to_date(struct_to_int(y, m, d));
}*/
struct st{int y, m, d;};
signed main(){
st a, b;
scanf("%lld%lld%lld", &a.y, &a.m, &a.d);
scanf("%lld%lld%lld", &b.y, &b.m, &b.d);
int aa = struct_to_int(a.y, a.m, a.d), bb = struct_to_int(b.y, b.m, b.d);
if(aa > bb)swap(a, b);
int ans = b.y-a.y;
if(a.m == b.m and a.d == b.d){
if(a.m == 2 and a.d == 29)printf("%lld\n", ans+1);
else printf("%lld\n", ans);
return 0;
}
if(b.m < a.m || (b.m <= a.m and b.d < a.d)){
printf("%lld\n", ans);
}else printf("%lld\n", ans+1);
//printf("%lld %lld %lld", a.y, a.m, a.d);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector< int > a(3), b(3);
cin >> a[0] >> a[1] >> a[2] >> b[0] >> b[1] >> b[2];
if(a > b) swap(a, b);
int add = 0;
if(a[1] < b[1]) ++add;
else if(a[1] == b[1] && a[2] < b[2]) ++add;
cout << b[0] - a[0] + add << endl;
} |
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<fixed<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod=998244353;
const llint moe=992186091;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
int ya,ma,da,yb,mb,db;cin>>ya>>ma>>da>>yb>>mb>>db;
da+=ma*100;
db+=mb*100;
if(ya*10000+da<yb*10000+db){swap(ya,yb);swap(da,db);}
int ans=ya-yb;
if(da>db){ans++;}
cout<<ans<<endl;
return 0;
}
|
#include <iostream>
using namespace std;
int main(){
int a,b,c,d,e,f;
cin>>a>>b>>c>>d>>e>>f;
c+=b*100;
f+=e*100;
if(a<d) cout<<(f>c?d-a+1:d-a)<<endl;
else if(a>d) cout<<(f<c?a-d+1:a-d)<<endl;
else cout<<(f==c?0:1)<<endl;
return 0;
}
|
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <functional>
using namespace std;
bool is_leapyear(int y) {
// if(y < 1000) return false; // ??
if(y % 400 == 0) return true;
if(y % 100 == 0) return false;
return y % 4 == 0;
}
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int main() {
int y1, m1, d1; cin >> y1 >> m1 >> d1;
int y2, m2, d2; cin >> y2 >> m2 >> d2;
m1--; m2--;
char buf[128];
sprintf(buf, "%04d%02d%02d", y1, m1, d1);
string s1 = buf;
sprintf(buf, "%04d%02d%02d", y2, m2, d2);
string s2 = buf;
if(s1 > s2) {
swap(y1, y2);
swap(m1, m2);
swap(d1, d2);
}
bool valid = false;
int age1 = -1, age2 = -1, cnt = 0;
auto change_age = [&](int &age, int y, int m, int d,
int yc, int mc, int dc) {
// 誕生日が 2/29
if(m+1 == 2 and d == 29) {
if(is_leapyear(yc)) {
if(mc+1 == 2 and dc == 29) age++;
}
else {
if(mc+1 == 3 and dc == 1) age++;
}
}
else {
if(m == mc and d == dc) age++;
}
};
int ans = 0;
for(int y=y1, m=m1, d=d1; cnt<200000; d++) {
// change month
if(d > days[m] + leap[m] * is_leapyear(y)) {
m++;
d = 1;
}
// change year
if(m == 12) {
y++;
m = 0;
}
change_age(age1, y1, m1, d1, y, m, d);
if(y == y2 and m == m2 and d == d2) valid = true;
if(valid) {
change_age(age2, y2, m2, d2, y, m, d);
cnt++;
ans = max(ans, age1 - age2);
}
}
cout << ans << endl;
return 0;
}
|
#include<stdio.h>
int main(void)
{
int y[2],m[2],d[2],y1,m1,d1;
scanf("%d %d %d",&y[0],&m[0],&d[0]);
scanf("%d %d %d",&y[1],&m[1],&d[1]);
if(y[0]>y[1]) y1=y[0],y[0]=y[1],y[1]=y1,
m1=m[0],m[0]=m[1],m[1]=m1,
d1=d[0],d[0]=d[1],d[1]=d1;
//printf("%d %d %d\n",y[0],m[0],d[0]);
//printf("%d %d %d\n",y[1],m[1],d[1]);
if(y[0]==y[1]&&m[0]==m[1]&&d[0]==d[1]) printf("0\n");
else if(y[0]==y[1]&&m[0]<=m[1]&&d[0]>d[1]||
y[0]==y[1]&&m[0]>m[1])
printf("1\n");
else if(y[0]<y[1]&&m[0]<m[1]&&d[0]<d[1])
printf("%d\n",y[1]);
else if(y[0]<y[1]&&m[0]<m[1])
printf("%d\n",y[1]-y[0]+1);
else if(y[0]<y[1]&&m[0]>=m[1])
printf("%d\n",y[1]-y[0]);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<int ,P> P3;
typedef pair<P ,P> PP;
const ll MOD = ll(1e9+7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-6;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), (v).end()
int main() {
int y[2], m[2], d[2];
REP(i,2){
cin >> y[i] >> m[i] >> d[i];
}
if(y[0] < y[1]){
swap(m[0],m[1]);
swap(d[0],d[1]);
}
if(m[0] == m[1] && d[0] == d[1] || y[0] != y[1] && m[0]*100+d[0] < m[1]*100+d[1]){
cout << abs(y[0]-y[1]) << endl;
}
else{
cout << abs(y[0]-y[1]) + 1 << endl;
}
return 0;
}
|
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
int main(){
int year_1,year_2,month_1,month_2,day_1,day_2;
scanf("%d %d %d",&year_1,&month_1,&day_1);
scanf("%d %d %d",&year_2,&month_2,&day_2);
if(month_1 == month_2){
if(day_1 == day_2){
printf("%d\n",abs(year_1-year_2));
}else if(day_1 < day_2){
if(year_1 < year_2){
printf("%d\n",abs(year_1-year_2)+1);
}else if(year_1 == year_2){
printf("1\n");
}else{ //year_1 > year_2
printf("%d\n",abs(year_1-year_2));
}
}else{ //day_1 > day_2
if(year_1 < year_2){
printf("%d\n",abs(year_1-year_2));
}else{ //year_1 >= year_2
printf("%d\n",abs(year_1-year_2)+1);
}
}
}else if(month_1 < month_2){
if(year_1 <= year_2){
printf("%d\n",abs(year_1-year_2)+1);
}else{ //year_1 >= year_2
printf("%d\n",abs(year_1-year_2));
}
}else{ //month_1 > month_2
if(year_1 < year_2){
printf("%d\n",abs(year_1-year_2));
}else{ //year_1 >= year_2
printf("%d\n",abs(year_1-year_2)+1);
}
}
return 0;
} |
#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld=long double;
int main() {
pair<int,pair<int,int>>p1,p2;
int a,b,c,d,e,f;cin>>a>>b>>c>>d>>e>>f;
p1=make_pair(a,make_pair(b,c));
p2=make_pair(d,make_pair(e,f));
if(p1>p2)swap(p1,p2);
int ysa=p2.first-p1.first;
int ans=0;
if (p1.second >= p2.second) {
ans=ysa;
}
else {
ans=ysa+1;
}
cout<<ans<<endl;
return 0;
} |
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
pair< int, pair<int,int> > p1, p2;
cin >> p1.first >> p1.second.first >> p1.second.second;
cin >> p2.first >> p2.second.first >> p2.second.second;
if ( p1 > p2 ) swap( p1, p2 );
if ( p1.second < p2.second )
cout << p2.first - p1.first + 1 << endl;
else cout << p2.first - p1.first << endl;
}
|
#include<stdio.h>
int main(void)
{
int y[2],m[2],d[2],y1,m1,d1;
scanf("%d %d %d",&y[0],&m[0],&d[0]);
scanf("%d %d %d",&y[1],&m[1],&d[1]);
if(y[0]>y[1]) y1=y[0],y[0]=y[1],y[1]=y1,
m1=m[0],m[0]=m[1],m[1]=m1,
d1=d[0],d[0]=d[1],d[1]=d1;
if(y[0]==y[1]&&m[0]==m[1]&&d[0]==d[1]) printf("0\n");
else if(y[0]==y[1]&&m[0]<=m[1]&&d[0]>d[1]||
y[0]==y[1]&&m[0]>m[1])
printf("1\n");
else if(y[0]<y[1]&&m[0]<m[1]&&d[0]<d[1])
printf("%d\n",y[1]);
else if(y[0]<y[1]&&m[0]<m[1])
printf("%d\n",y[1]-y[0]+1);
else if(y[0]<y[1]&&m[0]>=m[1])
printf("%d\n",y[1]-y[0]);
}
|
#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(int i=0;i<N;++i)
typedef long long int LL;
int main()
{
int y1, m1, d1, y2, m2, d2;
in >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
if (y1 > y2)
{
std::swap(y1, y2);
std::swap(m1, m2);
std::swap(d1, d2);
}
else if (y1 == y2)
{
if (m1 > m2)
{
std::swap(y1, y2);
std::swap(m1, m2);
std::swap(d1, d2);
}
else if (m1 == m2 && d1 > d2)
{
std::swap(y1, y2);
std::swap(m1, m2);
std::swap(d1, d2);
}
}
if (y1 == y2 && m1 == m2 && d1 == d2) out << 0 << std::endl;
else if (m1 < m2 || (m1 == m2 && d1 < d2)) out << y2 - y1 + 1 << std::endl;
else out << y2 - y1 << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int main(){
int y1,y2,m1,m2,d1,d2;
cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
int y=y1,d=d1,m=m1;
int e1=0,e2=y1-y2;
int ans=0;
if (m2>m1 || (m1==m2 && d1<d2)) e2--;
//cout << e2 << endl;
for (int t=0;t<400*366;t++){
d++;
if (m==4 || m==6 || m==9 || m==11){
if (d==31) {d=1;m++;}
}
else if (m==2){
if (y%4==0 && (y%100!=0 || y%400==0)) {
if (d==30) {d=1;m++;}
}
else {
if (d==29) {d=1;m++;}
}
}
else {
if (d==32) {d=1;m++;}
}
if (m==13) {d=1;m=1;y++;}
if (m==m1 && d==d1) e1++;
if (m==m2 && d==d2) e2++;
if (y%4!=0 || (y%100==0 && y%400!=0)) {
if (m1==2 && d1==29 && m==3 && d==1) e1++;
}
if (y%4!=0 || (y%100==0 && y%400!=0)) {
if (m2==2 && d2==29 && m==3 && d==1) e2++;
}
ans=max(ans,e1-e2);
ans=max(ans,e2-e1);
//cout << " " << ans << endl;
}
cout << ans << endl;
}
|
#include<iostream>
using namespace std;
int y1, m1, d1, y2, m2, d2;
int abss(int a)
{
if(a < 0)return -a;
else return a;
}
signed main()
{
cin >> y1 >> m1 >> d1;
cin >> y2 >> m2 >> d2;
if(y1 > y2){
swap(y1, y2);
swap(m1, m2);
swap(d1, d2);
}
else if(y1 == y2){
if(m1 > m2){
swap(y1, y2);
swap(m1, m2);
swap(d1, d2);
}
else if(m1 == m2){
if(d1 > d2){
swap(y1, y2);
swap(m1, m2);
swap(d1, d2);
}
}
}
if(m1 == m2 && d1 == d2){
cout << abss(y1 - y2) << endl;
}
else if(m1 > m2 || m1 == m2 && d1 > d2){
cout << abss(y1 - y2) << endl;
}
else{
cout << abss(y1 - y2) + 1 << endl;
}
return 0;
}
|
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;
pair<int, pair<int, int>>p1, p2;
int main(void)
{
cin >> p1.first >> p1.second.first >> p1.second.second;
cin >> p2.first >> p2.second.first >> p2.second.second;
if (p1 > p2)swap(p1, p2);
if (p1.second < p2.second) {
cout << p2.first - p1.first + 1 << endl;
}
else {
cout << p2.first - p1.first << endl;
}
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int main() {
pair< int, pair<int,int> > p1, p2;
cin >> p1.first >> p1.second.first >> p1.second.second;
cin >> p2.first >> p2.second.first >> p2.second.second;
if ( p1 > p2 ) swap( p1, p2 );
if ( p1.second < p2.second ) cout << p2.first - p1.first + 1 << endl;
else cout << p2.first - p1.first << endl;
return 0;
}
|
#include<iostream>
using namespace std;
int main(){
pair<int, pair<int, int> > y1, y2;
cin >> y1.first >> y1.second.first >> y1.second.second;
cin >> y2.first >> y2.second.first >> y2.second.second;
if ( y1 > y2 ) swap(y1, y2);
cout << y2.first - y1.first + (y1.second < y2.second ? 1 : 0) << endl;
}
|
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define rep(i,a,n) for(ll i =(a);i<(n);++i)
#define urep(i,a,n) for(ll i = (a);i>=(n);--i)
#define all(x) (x).begin(),(x).end()
#define INF 1e18
const int mod = 1e9 + 7;
typedef long long ll;
using namespace std;
ll dx[4] = { 1,-1,0,0 };
ll dy[4] = { 0,0,1,-1 };
ll N,M,X,Y,A,B,C,Q,K,R,W,H;
string S,T;
ll ans;
ll x[1000000];
ll y[1000000];
ll a[1000000];
ll b[1000000];
ll c[1000000];
ll gcd(ll a, ll b) {
if (b == 0)return a;
else return gcd(b, a%b);
}
struct Edge{
ll to,cost;
Edge(int to, int cost) : to(to), cost(cost) {}
};
typedef vector<vector<Edge> > AdjList;
AdjList graph;
vector<ll> dist;
int main() {
ll y1,y2,m1,m2,d1,d2;
cin>>y1>>m1>>d1>>y2>>m2>>d2;
if(y1>y2){
swap(y1,y2);
swap(m1,m2);
swap(d1,d2);
}
if(y1==y2){
if(m1==m2 && d1==d2)cout<<0<<endl;
else cout<<1<<endl;
return 0;
}
if(m1>m2 || (m1==m2 && d1>=d2)){
cout<<y2-y1<<endl;
}
else{
cout<<y2-y1+1<<endl;
}
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);
using namespace std;
namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;
int main(){
cin.tie(0); cin.sync_with_stdio(0);
tuple<int,int,int> a, b, now;
int x, y, z;
cin >> x >> y >> z; a = tie(x, y, z);
cin >> x >> y >> z; b = tie(x, y, z);
if(a > b) swap(a, b);
if(a == b){
cout << 0 << endl;
return 0;
}
auto inc = [](tuple<int,int,int> &x){
auto numd = [](int y, int m){
const int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(m != 2) return a[m - 1];
if(y % 400 == 0 || y % 4 == 0 && y % 100) return 29;
return 28;
};
int y, m, d; tie(y, m, d) = x;
if(++d > numd(y, m)){
d = 1;
if(++m > 12) m = 1, y++;
}
x = tie(y, m, d);
};
auto ok = [](tuple<int,int,int> b, tuple<int,int,int> a){
if(get<1>(a) == get<1>(b) && get<2>(a) == get<2>(b)) return 1;
if(get<1>(a) == 2 && get<2>(a) == 29 &&
get<1>(b) == 3 && get<2>(b) == 1 &&
!(get<0>(b) % 400 == 0 || get<0>(b) % 4 == 0 && get<0>(b) % 100))
return 1;
return 0;
};
now = a;
int ya = 0, yb = 0, ans = 0;
while(now < b){
inc(now);
if(ok(now, a)) ya++;
ans = max(ans, ya - yb);
}
//dbg(get<0>(now),get<1>(now),get<2>(now));
rep(it, 365 * 4400){
inc(now);
if(ok(now, a)) ya++;
if(ok(now, b)) yb++;
ans = max(ans, ya - yb);
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
int main()
{
pair<int, pair<int, int>> d1, d2;
cin >> d1.first >> d1.second.first >> d1.second.second >> d2.first >> d2.second.first >> d2.second.second;
if (d1 > d2)
swap(d1, d2);
int y = d2.first - d1.first;
if (d1.second >= d2.second)
cout << y << endl;
else
cout << y + 1 << endl;
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
#include<functional>
#include<map>
#include<unordered_set>
using namespace std;
//int p = 998244353;
int p = 1000000007;
#define vel vector<int>
#define vvel vector<vel>
#define rep(i,n) for(int i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define qin pair<int,pin>
#define V vector
#define Endl endl
#define veb vector<bool>
#define rev(s) reverse(s.begin(),s.end())
int max_kai = 150000;
vel kai(max_kai, 1);
vel inv_kai;
int rui(int a, int n) {
if (n == 0) { return 1; }
int x = rui(a, n / 2);
x *= x; x %= p;
if (n % 2 == 1) { x *= a; x %= p; }
return x;
}
int inv(int a) { return rui(a, p - 2); }
void make_kai() {
for (int i = 1; i < max_kai; i++) { kai[i] = kai[i - 1] * i; kai[i] %= p; }
inv_kai = kai;
rep(i, max_kai) { inv_kai[i] = inv(kai[i]); }
}
int nCr(int n, int r) {
int a = kai[n] * inv_kai[r]; a %= p;
a *= inv_kai[n - r]; a %= p;
return a;
}
vel pa;
int root(int x) {
if (pa[x] == -1) { return x; }
int ans = root(pa[x]); pa[x] = ans;
return ans;
}
void marge(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {pa[x] = y;}
}
#define lower(h,val) lower_bound(h.begin(),h.end(),val)-h.begin()
#define upper(h,val) upper_bound(h.begin(),h.end(),val)-h.begin()
int num_lp(int a, vel &nex) {
unordered_set<int> se;
int ans = 0;
while (true) {
if (se.find(a) != se.end()) {
break;
}
se.insert(a);
a = nex[a];
ans++;
}
return ans;
}
int find_cr(int a, vel &nex) {
unordered_set<int> se;
while (true) {
if (se.find(a) != se.end()) {break;}
se.insert(a);
a = nex[a];
}
return num_lp(a, nex);
}
int inf = 2000000000;
vvel dp;
int solve(vvel &sum, int i, int j, bool is_f) {
int h = sum.size(); h--;
int w = sum[0].size();
if (i == h || j == w) { return 0; }
if (dp[i][j] == inf) {
int ans1 = solve(sum, i + 1, j, !is_f);
int ans2 = solve(sum, i, j + 1, !is_f);
ans2 += sum[i][j];
if (is_f) {
dp[i][j] = min(ans1, ans2);
}
else {
dp[i][j] = max(ans1, ans2);
}
}
return dp[i][j];
}
int rui10(int a) {
if (a == 0) { return 1; }
return 10 * rui10(a - 1);
}
int gcd(int a, int b) {
if (a < b) { swap(a, b); }
if (b == 0) { return a; }
return gcd(b, a%b);
}
signed main(){
vel a(3), b(3);
cin >> a[0] >> a[1] >> a[2] >> b[0] >> b[1] >> b[2];
if (a > b) { swap(a, b); }
if (a[1]<b[1] || (a[1]==b[1]&&a[2]<b[2])){
cout << b[0] - a[0] + 1 << endl;
}
else {
cout << b[0] - a[0] << endl;
}
return 0;
}
|
#include <stdio.h>
int main(void) {
int y1, m1, d1, y2, m2, d2, ans;
scanf("%d%d%d%d%d%d", &y1, &m1, &d1, &y2, &m2, &d2);
if(y1 > y2) {
int t;
t = y1, y1 = y2, y2 = t;
t = m1, m1 = m2, m2 = t;
t = d1, d1 = d2, d2 = t;
}
if(y1 != y2) {
ans = y2 - y1;
if(m1 < m2) printf("%d\n", ans + 1);
else if(m1 > m2) printf("%d\n", ans);
else {
if(d1 < d2) printf("%d\n", ans + 1);
else printf("%d\n", ans);
}
} else {
if(m1 == m2 && d1 == d2) printf("0\n");
else printf("1\n");
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int y[2], m[2], d[2];
int nowd, nowm;
cin >> y[0] >> m[0] >> d[0];
cin >> y[1] >> m[1] >> d[1];
if(d[0] == d[1] && m[0] == m[1]){ // 誕生日が一緒
cout << abs(y[0] - y[1]) << endl;
}else{
int ans = abs(y[0] - y[1]);
if(y[0] > y[1]){
if(m[0] > m[1]) ++ans;
else if(m[0] == m[1] && d[0] > d[1]) ++ans;
}else if(y[0] < y[1]){
if(m[1] > m[0]) ++ans;
else if(m[0] == m[1] && d[1] > d[0]) ++ans;
}else{
++ans;
}
cout << ans << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
int y1, y2, m1, m2, d1, d2, ans;
bool isOld1 = false;
int main() {
cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
if (y1 < y2) {
isOld1 = true;
} else if (y1 == y2) {
if (m1 < m2) {
isOld1 = true;
} else if (m1 == m2) {
if (d1 < d2) {
isOld1 = true;
} else if (d1 == d2) {
cout << '0' << '\n';
return 0;
}
}
}
if (isOld1) {
int tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
tmp = m1;
m1 = m2;
m2 = tmp;
tmp = d1;
d1 = d2;
d2 = tmp;
}
ans = y1 - y2;
if ((m2 - m1) < 0 || (m2 - m1) == 0 && (d2 - d1) < 0) {
ans++;
}
cout << ans << '\n';
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
signed main(){
int y1,m1,d1;
cin>>y1>>m1>>d1;
int y2,m2,d2;
cin>>y2>>m2>>d2;
auto sw=[&](){
swap(y1,y2);swap(m1,m2);swap(d1,d2);
};
if(m1>m2) sw();
if(m1==m2&&d1>d2) sw();
if(m1==m2&&d1==d2){
cout<<abs(y1-y2)<<endl;
return 0;
}
cout<<abs(y2-y1)+(y1<=y2)<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define REP(i, n) FOR(i, 0, n)
#define LAR(a, b) ((a)=max((a),(b)))
#define SML(a, b) ((b)=max((a),(b)))
void next(int &y, int &m, int &d){
if(m==12 && d==31){
y++;
m=d=1;
return;
}else if(m==2){
if(y%4==0 && y%100!=0 || y%400==0){
if(d==29){
m++;
d=1;
return;
}
}else{
if(d==28){
m++;
d=1;
return;
}
}
}else if(m==4 || m==6 || m==9 || m==11){
if(d==30){
m++;
d=1;
return;
}
}else{
if(d==31){
m++;
d=1;
return;
}
}
d++;
}
int pls(int bm, int bd, int y, int m, int d){
if(bm==2 && bd==29){
if(y%4==0 && y%100!=0 || y%400==0){
}else{
if(m==3 && d==1) return 1;
}
}
if(bm==m && bd==d) return 1;
else return 0;
}
int main(){
int a,b,c,d,e,f;
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
int p,q,r,x,y;
aaa:
p=a; q=b; r=c;
x=0; y=0;
while(p!=d || q!=e || r!=f){
if(p>d){
swap(a,d);
swap(b,e);
swap(c,f);
goto aaa;
}
next(p,q,r);
x+=pls(b,c,p,q,r);
}
int ans=0;
REP(i, 10000){
next(p,q,r);
x+=pls(b,c,p,q,r);
y+=pls(e,f,p,q,r);
LAR(ans,x-y);
}
printf("%d\n", ans);
} |
/*
∧_∧ にっぽんちゃちゃちゃ
/\( ・∀・)/ヽ
( ● と つ ● ) にっぽんちゃちゃちゃ
\/⊂、 ノ \ノ
し'
*/
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define repone(i,n) for(int i=1;i<(int)(n);++i)
#define repo(i,o,n) for(int i=o;i<(int)(n);++i)
#define Rep(j,n) for(int j=0;j<(int)(n);++j)
#define Repo(j,o,n) for(int j=o;j<(int)(n);++j)
#define Repone(j,n) for(int j=1;j<(int)(n);++j)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define int long long
#define MOD 1000000007
#define INF 9223372036854775807
signed main() {
pair< int, pair<int,int> > p1, p2;
cin >> p1.first >> p1.second.first >> p1.second.second;
cin >> p2.first >> p2.second.first >> p2.second.second;
if ( p1 > p2 ) swap( p1, p2 );
if ( p1.second < p2.second )
cout << p2.first - p1.first + 1 << endl;
else cout << p2.first - p1.first << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) a.erase(unique(all(a)),a.end())
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
int main(){
vi y(2),m(2),d(2);
rep(i,2){
cin>>y[i]>>m[i]>>d[i];
}
if(y[0]==y[1]){
if(m[0]==m[1]&&d[0]==d[1])dame(0);
dame(1);
}
ll a=0,b=1;
if(y[0]>y[1])swap(a,b);
ll ans=y[b]-y[a];
if(m[b]>m[a])ans++;
if(m[b]==m[a]&&d[b]>d[a])ans++;
out(ans);
}
|
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
template<typename T>
using vec = vector<T>;
template<typename T>
using mat = vector<vec<T>>;
template<typename T>
using val = valarray<T>;
using pi = pair<int,int>;
#define rep(a,b) for(int a=0; a<b; ++a)
#define Rep(a,b,c) for(int a=b; a<c; ++a)
#define ran(a,b) for(auto &a:b)
#define all(v) v.begin(),v.end()
struct S {
int y, m, d;
};
int main(){
S s1, s2;
cin >> s1.y >> s1.m >> s1.d;
cin >> s2.y >> s2.m >> s2.d;
if(s1.y == s2.y) {
if(s1.m == s2.m && s1.d == s2.d) cout << 0 << endl;
else cout << 1 << endl;
return 0;
} else if(s1.y > s2.y) swap(s1, s2);
if(s1.m == s2.m && s1.d == s2.d) cout << 0 << endl;
else if(s1.m < s2.m || (s1.m == s2.m && s1.d < s2.d)) cout << s2.y - s1.y + 1 << endl;
else cout << s2.y - s1.y << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
typedef long long ll;
typedef pair<int,int> pii;
#define _overload4(_1,_2,_3,_4,name,...) name
#define _overload3(_1,_2,_3,name,...) name
#define _rep1(n) _rep2(i,n)
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(ll i=a;i<b;++i)
#define _rep4(i,a,b,c) for(ll i=a;i<b;i+=c)
#define rep(...) _overload4(__VA_ARGS__,_rep4,_rep3,_rep2,_rep1)(__VA_ARGS__)
#define _rrep1(n) _rrep2(i,n)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(ll i=b-1;i>=a;i--)
#define _rrep4(i,a,b,c) for(ll i=a+(b-a-1)/c*c;i>=a;i-=c)
#define rrep(...) _overload4(__VA_ARGS__,_rrep4,_rrep3,_rrep2,_rrep1)(__VA_ARGS__)
#define each(i,a) for(auto&& i:a)
#define sum(...) accumulate(range(__VA_ARGS__),0LL)
#define _range(i) (i).begin(),(i).end()
#define _range2(i,k) (i).begin(),(i).begin()+k
#define _range3(i,a,b) (i).begin()+a,(i).begin()+b
#define range(...) _overload3(__VA_ARGS__,_range3,_range2,_range)(__VA_ARGS__)
const ll LINF=0x3fffffffffffffff;
const ll MOD=1000000007;
const ll MODD=0x3b800001;
const int INF=0x3fffffff;
#define yes(i) out(i?"yes":"no")
#define Yes(i) out(i?"Yes":"No")
#define YES(i) out(i?"YES":"NO")
#define Possible(i)out(i?"Possible":"Impossible")
#define elif else if
#define unless(a) if(!(a))
//#define START auto start=system_clock::now()
//#define END auto end=system_clock::now();cerr<<duration_cast<milliseconds>(end-start).count()<<" ms\n"
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;in(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;in(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;in(__VA_ARGS__)
#define vec(type,name,...) vector<type> name(__VA_ARGS__);
#define VEC(type,name,size) vector<type> name(size);in(name)
#define vv(type,name,h,w,value) vector<vector<type>>name(h,vector<type>(w,value))
#define pll pair<ll,ll>
struct SETTINGS{SETTINGS(){cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);};}SETTINGS;
template<typename T>
inline bool update_min(T& mn,const T& cnt){if(mn>cnt){mn=cnt;return 1;}else return 0;}
template<typename T>
inline bool update_max(T& mx,const T& cnt){if(mx<cnt){mx=cnt;return 1;}else return 0;}
template<typename T>
inline T max(vector<T>& vec){return *max_element(range(vec));}
template<typename T>
inline constexpr T gcd (T a,T b) {if(a==b)return a;else return gcd(b,(a-1)%b+1);}
inline void in() {}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec);
template<typename T,size_t size>
istream& operator >> (istream& is, array<T,size>& vec);
template<typename T,typename L>
istream& operator >> (istream& is, pair<T,L>& p);
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec);
template<typename T,typename L>
ostream& operator << (ostream& os, pair<T,L>& p);
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){for(T& x: vec) is >> x;return is;}
template<typename T,typename L>
istream& operator >> (istream& is, pair<T,L>& p){is >> p.first;is >> p.second;return is;}
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec){os << vec[0];rep(i,1,vec.size()){os << ' ' << vec[i];}return os;}
template<typename T>
ostream& operator << (ostream& os, deque<T>& vec){os << vec[0];rep(i,1,vec.size()){os << ' ' << vec[i];}return os;}
template<typename T,typename L>
ostream& operator << (ostream& os, pair<T,L>& p){os << p.first << " " << p.second;return os;}
template <class Head, class... Tail>
inline void in(Head&& head,Tail&&... tail){cin>>head;in(move(tail)...);}
template <typename T>
inline void out(T t){cout<<t<<'\n';}
inline void out(){cout<<'\n';}
template <class Head, class... Tail>
inline void out(Head head,Tail... tail){cout<<head<<' ';out(move(tail)...);}
signed main(){
VEC(int,a,3);
VEC(int,b,3);
if(a>b)swap(a,b);
if(a[1]<b[1])out(b[0]-a[0]+1);
elif(a[1]>b[1])out(b[0]-a[0]);
elif(a[2]<b[2])out(b[0]-a[0]+1);
else out(b[0]-a[0]);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
array<tuple<int, int, int>, 2> birth;
for(int i = 0; i < 2; ++i) {
cin >> get<0>(birth[i]) >> get<1>(birth[i]) >> get<2>(birth[i]);
}
if(birth[0] > birth[1]) swap(birth[0], birth[1]);
int ans = get<0>(birth[1]) - get<0>(birth[0]);
if(make_pair(get<1>(birth[0]), get<2>(birth[0])) < make_pair(get<1>(birth[1]), get<2>(birth[1]))) {
ans += 1;
}
cout << ans << endl;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
using namespace std;
#define ll long long
const ll linf = 1e15;
struct edge{
int to;
ll cost;
};
struct node{
int from;
ll cost;
bool operator<(const node &n1) const {
return n1.cost < cost;
}
};
int N, M, S, T, U, V;
vector<edge> G[100010];
ll DU[100010], DV[100010], DS[100010], dp[3][100010];
bool used[100010];
void dijk(int s, ll* D){
fill(D, D + N + 1, linf);
priority_queue<node> pq;
pq.push(node{s, 0});
D[s] = 0;
while(!pq.empty()){
node n1 = pq.top();
pq.pop();
if(D[n1.from] < n1.cost) continue;
for(auto u : G[n1.from]){
ll cost = u.cost + n1.cost;
if(cost < D[u.to]){
D[u.to] = cost;
pq.push(node{u.to, cost});
}
}
}
}
ll solve(){
dijk(U, DU), dijk(V, DV), dijk(S, DS);
for(int i = 1; i <= N ; i++){
dp[0][i] = DU[i];
dp[1][i] = DV[i];
dp[2][i] = linf;
}
priority_queue<node> pq;
pq.push(node{S, 0});
while(!pq.empty()){
node n1 = pq.top();
pq.pop();
for(auto u : G[n1.from]){
ll cost = u.cost + n1.cost;
if(DS[u.to] < cost) continue;
dp[0][u.to] = min(dp[0][u.to], dp[0][n1.from]);
dp[1][u.to] = min(dp[1][u.to], dp[1][n1.from]);
if(!used[u.to]){
used[u.to] = true;
pq.push(node{u.to, DS[u.to]});
}
}
}
for(int i = 1; i <= N; i++){
dp[2][i] = min(dp[0][i] + DV[i], dp[1][i] + DU[i]);
}
fill(used, used + N + 1, false);
pq.push(node{S, 0});
while(!pq.empty()){
node n1 = pq.top();
pq.pop();
for(auto u : G[n1.from]){
ll cost = u.cost + n1.cost;
if(DS[u.to] < cost) continue;
dp[2][u.to] = min(dp[2][u.to], dp[2][n1.from]);
if(!used[u.to]){
used[u.to] = true;
pq.push(node{u.to, DS[u.to]});
}
}
}
return min(dp[2][T], DU[V]);
}
void init(){
cin >> N >> M >> S >> T >> U >> V;
for(int i = 0; i < M; i++){
int A, B;
ll C;
cin >> A >> B >> C;
G[A].push_back(edge{B, C});
G[B].push_back(edge{A, C});
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
init();
cout << solve() << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define vi vector<int>
#define pb push_back
#define i197 1000000007
#define MAX_N 200002
using namespace std;
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<Pii,int> Piii;
const ll inf=100000000000000000;
struct edge{
int myc,to,c,cost;
};
struct pos2{
int to,from;
ll cost;
};
struct pos{
int to;
ll cost;
};
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
vector<pos> vec[100001];
ll dpV[100001];
ll dpU[100001];
ll dp[100001][4];
int main() {
int n,m;
int s,t,u,v;
cin>>n>>m>>s>>t>>u>>v;
rep(i,m){
int a,b;
ll c;
cin>>a>>b>>c;
vec[a].push_back(pos{b,c});
vec[b].push_back(pos{a,c});
}
rep(i,n){
dpU[i+1]=inf;
dpV[i+1]=inf;
dp[i+1][0]=inf;
dp[i+1][1]=inf;
dp[i+1][2]=inf;
dp[i+1][3]=inf;
}
auto calc = [](pos2 l,pos2 r){return l.cost > r.cost;};
priority_queue<pos2,vector<pos2>,decltype(calc)> que(calc);
que.push(pos2{v,v,0});
while(!que.empty()){
pos2 p2=que.top();que.pop();
if(p2.cost>=dpV[p2.to])continue;
dpV[p2.to]=p2.cost;
for(auto it:vec[p2.to]){
que.push(pos2{it.to,p2.to,p2.cost+it.cost});
}
}
que.push(pos2{u,u,0});
while(!que.empty()){
pos2 p2=que.top();que.pop();
if(p2.cost>=dpU[p2.to])continue;
dpU[p2.to]=p2.cost;
for(auto it:vec[p2.to]){
que.push(pos2{it.to,p2.to,p2.cost+it.cost});
}
}
ll r=dpU[v];
que.push(pos2{s,s,0});
while(!que.empty()){
pos2 p2=que.top();que.pop();
if(p2.cost>dp[p2.to][0])continue;
dp[p2.to][1]=min(dp[p2.to][1],min(dp[p2.from][1],dpV[p2.to]));
dp[p2.to][2]=min(dp[p2.to][2],min(dp[p2.from][2],dpU[p2.to]));
dp[p2.to][3]=min(min(dp[p2.to][3],dp[p2.from][3]),min(dp[p2.to][1]+dpU[p2.to],dp[p2.to][2]+dpV[p2.to]));
if(dp[p2.to][0]==p2.cost)continue;
dp[p2.to][0]=p2.cost;
for(auto it:vec[p2.to]){
que.push(pos2{it.to,p2.to,p2.cost+it.cost});
}
}
// rep(i,n)cout<<dpU[i+1]<<endl;
cout<<min(r,dp[t][3])<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
using namespace std;
using ll=int_fast64_t;
using P=pair<ll,ll>;
const ll INF=1LL<<30;
const ll LINF=1LL<<61;
const double EPS=1e-9;
const int MOD=1e9+7;
struct edge{int to;ll cost;};
int main(){
int N,M;cin>>N>>M;
int S,T;cin>>S>>T;--S;--T;
int U,V;cin>>U>>V;--U;--V;
vector<vector<edge>> G(N),nG(N);
for(int i=0;i<M;i++){
int a,b;ll c;cin>>a>>b>>c;--a;--b;
G[a].push_back({b,c});
G[b].push_back({a,c});
}
vector<ll> ds(N,LINF),dt(N,LINF),du(N,LINF),dv(N,LINF);
ds[S]=0,dt[T]=0,du[U]=0,dv[V]=0;
priority_queue<P,vector<P>,greater<P>> q;
q.push(P(0,S));
while(!q.empty()){
int v=q.top().second;q.pop();
for(auto e:G[v]){
if(ds[e.to]<=ds[v]+e.cost)continue;
ds[e.to]=ds[v]+e.cost;
q.push(P(ds[e.to],e.to));
}
}
q.push(P(0,T));
while(!q.empty()){
int v=q.top().second;q.pop();
for(auto e:G[v]){
if(dt[e.to]<=dt[v]+e.cost)continue;
dt[e.to]=dt[v]+e.cost;
q.push(P(dt[e.to],e.to));
}
}
q.push(P(0,U));
while(!q.empty()){
int v=q.top().second;q.pop();
for(auto e:G[v]){
if(du[e.to]<=du[v]+e.cost)continue;
du[e.to]=du[v]+e.cost;
q.push(P(du[e.to],e.to));
}
}
q.push(P(0,V));
while(!q.empty()){
int v=q.top().second;q.pop();
for(auto e:G[v]){
if(dv[e.to]<=dv[v]+e.cost)continue;
dv[e.to]=dv[v]+e.cost;
q.push(P(dv[e.to],e.to));
}
}
vector<int> in(N),vs;
for(int i=0;i<N;i++){
for(auto e:G[i]){
if(ds[i]+e.cost+dt[e.to]==ds[T]){
nG[i].push_back(e);
in[e.to]++;
}
}
}
stack<int> st;
for(int i=0;i<N;i++){
if(in[i]==0&&nG[i].size()){
st.push(i);
}
}
while(!st.empty()){
int v=st.top();st.pop();
vs.push_back(v);
for(auto e:nG[v]){
in[e.to]--;
if(in[e.to]==0)st.push(e.to);
}
}
ll ans=du[V];
vector<P> dp(N,P(LINF,LINF));
for(auto v:vs){
dp[v].first=min(dp[v].first,du[v]);
dp[v].second=min(dp[v].second,dv[v]);
ans=min(ans,dp[v].first+dv[v]);
ans=min(ans,dp[v].second+du[v]);
for(auto e:nG[v]){
dp[e.to].first=min(dp[e.to].first,dp[v].first);
dp[e.to].second=min(dp[e.to].second,dp[v].second);
}
}
cout<<ans<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template <typename T>
vector<T> dijkstra(Int s,vector<vector<pair<Int, T> > > & G,T INF){
using P = pair<T, Int>;
Int n=G.size();
vector<T> d(n,INF);
vector<Int> b(n,-1);
priority_queue<P,vector<P>,greater<P> > q;
d[s]=0;
q.emplace(d[s],s);
while(!q.empty()){
P p=q.top();q.pop();
Int v=p.second;
if(d[v]<p.first) continue;
for(auto& e:G[v]){
Int u=e.first;
T c=e.second;
if(d[u]>d[v]+c){
d[u]=d[v]+c;
b[u]=v;
q.emplace(d[u],u);
}
}
}
return d;
}
template<typename T> void chmin(T &a,T b){if(a>b) a=b;}
template<typename T> void chmax(T &a,T b){if(a<b) a=b;}
template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T>
vector<vector<T> > make_v(size_t a,size_t b){
return vector<vector<T> >(a,make_v<T>(b));
}
template<typename T>
vector<vector<vector<T> > > make_v(size_t a,size_t b,size_t c){
return vector<vector<vector<T> > > (a,make_v<T>(b,c));
}
template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}
template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
for(auto &e:t) fill_v(e,v);
}
//INSERT ABOVE HERE
signed main(){
Int n,m,s,t,U,V;
cin>>n>>m>>s>>t>>U>>V;
s--;t--;U--;V--;
const Int INF=1e16;
using P = pair<Int, Int>;
vector<vector<P> > G(n);
for(Int i=0;i<m;i++){
Int a,b,c;
cin>>a>>b>>c;
a--;b--;
G[a].emplace_back(b,c);
G[b].emplace_back(a,c);
}
auto ds=dijkstra(s,G,INF);
auto dt=dijkstra(t,G,INF);
assert(ds[t]==dt[s]);
Int L=ds[t];
using TP=tuple<Int, Int, Int>;
vector<vector<TP> > T(n);
for(Int v=0;v<n;v++){
for(auto e:G[v]){
Int u,c;
tie(u,c)=e;
if(ds[v]+dt[v]==L&&ds[u]+dt[u]==L&&
(ds[u]+c==ds[v]||ds[v]+c==ds[u]))
T[v].emplace_back(u,c,(ds[u]+c==ds[v])+1);
else T[v].emplace_back(u,c,0);
}
}
auto dp=make_v<Int>(4,n);
fill_v(dp,INF);
priority_queue<TP,vector<TP>,greater<TP> > pq;
dp[0][U]=0;
pq.emplace(dp[0][U],0,U);
while(!pq.empty()){
Int d,c,v;
tie(d,c,v)=pq.top();pq.pop();
if(dp[c][v]<d) continue;
//cout<<d<<" "<<c<<" "<<v<<endl;
for(auto e:T[v]){
Int u,nc,nd;
tie(u,nc,nd)=e;
Int nt=-1;
if(c==0){
if(nd){
nt=nd;
if(dp[nt][u]>d){
dp[nt][u]=d;
pq.emplace(dp[nt][u],nt,u);
}
}
nt=c;
if(dp[nt][u]>d+nc){
dp[nt][u]=d+nc;
pq.emplace(dp[nt][u],nt,u);
}
}
if(c==1||c==2){
if(c==nd){
nt=c;
if(dp[nt][u]>d){
dp[nt][u]=d;
pq.emplace(dp[nt][u],nt,u);
}
}
nt=3;
if(dp[nt][u]>d+nc){
dp[nt][u]=d+nc;
pq.emplace(dp[nt][u],nt,u);
}
}
if(c==3){
nt=3;
if(dp[nt][u]>d+nc){
dp[nt][u]=d+nc;
pq.emplace(dp[nt][u],nt,u);
}
}
}
}
cout<<min({dp[0][V],dp[1][V],dp[2][V],dp[3][V]})<<endl;
return 0;
}
|
# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;
constexpr long long INF = numeric_limits<LL>::max();
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourth; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
LL h, w, n, m, k, t, s, p, q, last, first, cnt, sum, ans, dp[4][200000];
Pll a[400000], b[400000];
vector<LL> d, dp1, d_p, d_q;
string str, ss;
bool f;
char c[3000][3000];
int di[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
struct Edge { LL to, cost; };
struct edge { LL from, to, cost; };
vector<vector<Edge>>vec;
vector<edge>ed;
//vector<LL>v;
map<Pll, LL>ma;
multiset<LL>st;
void YN(bool f) {
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void yn(bool f) {
if (f)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
vector<LL> dijk(vector<vector<Edge>> &g, LL s) {
vector<LL>dis(g.size());
rep(i, g.size()) {
dis[i] = INF;
}
dis[s] = 0;
priority_queue<Pll, vector<Pll>, greater<Pll>>pq;
pq.push(Pll(0, s));
while (!pq.empty()) {
Pll num = pq.top();
pq.pop();
LL v = num.sec, cost = num.fir;
if (dis[v] < cost)continue;
rep(i, g[v].size()) {
if (dis[g[v][i].to] > dis[v] + g[v][i].cost) {
dis[g[v][i].to] = dis[v] + g[v][i].cost;
pq.push(Pll(dis[g[v][i].to], g[v][i].to));
}
}
}
return dis;
}
LL dfs(LL s, LL p_cos, LL q_cos) {
LL ret = min({ p_cos + d_q[s], q_cos + d_p[s], p_cos + q_cos });
rep(i, vec[s].size()) {
LL next = vec[s][i].to;
if (d[s] <= d[next])continue;
if (d[s] == d[next] + vec[s][i].cost) {
ret = min(ret, dfs(next, min(p_cos, d_p[next]), min(q_cos, d_q[next])));
}
}
return ret;
}
int main() {
cin >> n >> m;
cin >> s >> t;
cin >> p >> q;
s--, t--, p--, q--;
vec.resize(n);
rep(i, m) {
LL x, y, z;
cin >> x >> y >> z;
x--, y--;
vec[x].push_back(Edge{ y,z });
vec[y].push_back(Edge{ x,z });
ed.push_back(edge{ x,y,z });
}
rep(i, 4) {
rep(j, n)dp[i][j] = INF;
}
d = dijk(vec, s);
d_p = dijk(vec, p);
d_q = dijk(vec, q);
priority_queue<Pll, vector<Pll>, greater<Pll>>pq;
rep(i, m) {
pq.push(Pll(min(d[ed[i].to], d[ed[i].from]), i));
}
dp[0][s] = d_p[s];
dp[1][s] = d_q[s];
dp[2][s] = d_p[s] + d_q[s];
while (!pq.empty()) {
Pll now = pq.top();
pq.pop();
LL i = now.second, u = ed[i].to, v = ed[i].from;
rep(j, 2) {
if (d[u] + ed[i].cost == d[v]) {
if (dp[0][u] + dp[1][u] < dp[2][v]) {
dp[0][v] = min({ dp[0][u], d_p[v],dp[0][v] });
dp[1][v] = min({ dp[1][u], d_q[v], dp[1][v] });
}
}
swap(u, v);
}
}
rep(i, m) {
pq.push(Pll(min(d[ed[i].to], d[ed[i].from]), i));
}
while (!pq.empty()) {
Pll now = pq.top();
pq.pop();
LL i = now.second, u = ed[i].to, v = ed[i].from;
rep(j, 2) {
if (d[u] + ed[i].cost == d[v]) {
if (min({ dp[0][u], d_p[v] }) + min({ dp[1][u], d_q[v] }) < dp[2][v]) {
dp[0][v] = min({ dp[0][u], d_p[v] });
dp[1][v] = min({ dp[1][u], d_q[v] });
dp[2][v] = dp[0][v] + dp[1][v];
}
}
swap(u, v);
}
}
//dp[0]+dp[1] uにpから,vにqから
//cout << min(dfs(t, d_p[t], d_q[t]), d_p[q]) << endl;
cout << min({ dp[2][t], d_p[q] }) << endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define F first
#define S second
#define PB push_back
#define INF 100000000000000000
struct po{
ll v,c,r;
bool operator >(const po&q)const{
return c>q.c;
}
};
int n,m,s,t,p,q;
ll sd[100005],td[100005],dp[100005][4],d;
vector<P>g[100005];
priority_queue<po,vector<po>,greater<po> >dik;
int main(void){
scanf("%d%d%d%d%d%d",&n,&m,&s,&t,&p,&q);
s--,t--,p--,q--;
for(int i=0;i<n;i++){
sd[i]=INF;
td[i]=INF;
dp[i][0]=INF;
dp[i][1]=INF;
dp[i][2]=INF;
dp[i][3]=INF;
}
for(int i=0;i<m;i++){
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
g[--a].PB(P(--b,c));
g[b].PB(P(a,c));
}
sd[s]=0;
td[t]=0;
dik.push(po{s,0,0});
dik.push(po{t,0,1});
while(!dik.empty()){
ll v=dik.top().v,c=dik.top().c,r=dik.top().r;
dik.pop();
if(r==0){
if(sd[v]<c)continue;
for(int i=0;i<g[v].size();i++){
ll u=g[v][i].F,e=g[v][i].S;
if(sd[u]>c+e){
sd[u]=c+e;
dik.push(po{u,c+e,0});
}
}
}else{
if(td[v]<c)continue;
for(int i=0;i<g[v].size();i++){
ll u=g[v][i].F,e=g[v][i].S;
if(td[u]>c+e){
td[u]=c+e;
dik.push(po{u,c+e,1});
}
}
}
}
d=sd[t];
dp[p][0]=0;
dik.push(po{p,0,0});
while(!dik.empty()){
ll v=dik.top().v,c=dik.top().c,r=dik.top().r;
dik.pop();
if(dp[v][r]<c)continue;
for(int i=0;i<g[v].size();i++){
ll u=g[v][i].F,e=g[v][i].S;
if((r==0||r==3)&&dp[u][r]>c+e){
dp[u][r]=c+e;
dik.push(po{u,c+e,r});
}
if((r==1||r==2)&&dp[u][3]>c+e){
dp[u][3]=c+e;
dik.push(po{u,c+e,3});
}
if((r==0||r==1)&&sd[v]+e+td[u]==d&&dp[u][1]>c){
dp[u][1]=c;
dik.push(po{u,c,1});
}
if((r==0||r==2)&&td[v]+e+sd[u]==d&&dp[u][2]>c){
dp[u][2]=c;
dik.push(po{u,c,2});
}
}
}
printf("%lld\n",min(min(dp[q][0],dp[q][1]),min(dp[q][2],dp[q][3])));
}
|
#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <algorithm>
using namespace std;
long long N, M, S, T, U, V, A[1 << 18], B[1 << 18], C[1 << 18], dist[1 << 18], dp1[1 << 18], dp2[1 << 18];
long long dista[1 << 18], distb[1 << 18], distc[1 << 18], distd[1 << 18];
vector<pair<long long, long long>>X[1 << 18];
int Order[1 << 18];
vector<long long>getdist(int pos) {
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>>Q;
for (int i = 1; i <= N; i++) dist[i] = (1LL << 60);
Q.push(make_pair(0, pos)); dist[pos] = 0;
while (!Q.empty()) {
int pos = Q.top().second; long long cost = Q.top().first; Q.pop();
for (int i = 0; i < X[pos].size(); i++) {
int to = X[pos][i].first; long long tm = X[pos][i].second;
if (dist[to] > cost + tm) {
dist[to] = cost + tm;
Q.push(make_pair(dist[to], to));
}
}
}
vector<long long>E;
for (int i = 1; i <= N; i++) E.push_back(dist[i]);
return E;
}
int main() {
cin >> N >> M >> S >> T >> U >> V;
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i] >> C[i];
X[A[i]].push_back(make_pair(B[i], C[i]));
X[B[i]].push_back(make_pair(A[i], C[i]));
}
vector<long long>A1 = getdist(S); for (int i = 1; i <= N; i++) dista[i] = A1[i - 1];
vector<long long>A2 = getdist(T); for (int i = 1; i <= N; i++) distb[i] = A2[i - 1];
vector<long long>A3 = getdist(U); for (int i = 1; i <= N; i++) distc[i] = A3[i - 1];
vector<long long>A4 = getdist(V); for (int i = 1; i <= N; i++) distd[i] = A4[i - 1];
vector<pair<long long, long long>>vec1;
for (int i = 1; i <= N; i++) { if (dista[i] + distb[i] == dista[T]) vec1.push_back(make_pair(dista[i], i)); }
sort(vec1.begin(), vec1.end());
for (int i = 1; i <= N; i++) { dp1[i] = (1LL << 60); dp2[i] = (1LL << 60); Order[i] = -1; }
for (int i = 0; i < vec1.size(); i++) Order[vec1[i].second] = i;
for (int i = 0; i < vec1.size(); i++) {
int pos = vec1[i].second; dp1[pos] = min(dp1[pos], distc[pos]);
for (int j = 0; j < X[pos].size(); j++) {
if (Order[X[pos][j].first] != -1 && Order[X[pos][j].first] > i) {
dp1[X[pos][j].first] = min(dp1[X[pos][j].first], dp1[pos]);
}
}
}
for (int i = vec1.size() - 1; i >= 0; i--) {
int pos = vec1[i].second; dp2[pos] = min(dp2[pos], distc[pos]);
for (int j = 0; j < X[pos].size(); j++) {
if (Order[X[pos][j].first] != -1 && Order[X[pos][j].first] < i) {
dp2[X[pos][j].first] = min(dp2[X[pos][j].first], dp2[pos]);
}
}
}
long long ans = distc[V];
for (int i = 1; i <= N; i++) ans = min(ans, min(dp1[i], dp2[i]) + distd[i]);
cout << ans << endl;
return 0;
}
|
#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define PB push_back
#define MP make_pair
int N, M, S, T, U, V;
vector<pair<int, LL>> edge[100000];
LL C[200000];
LL cost[100000][3]; //from S, U, V
void dijkstra(){
for(int i=0; i<N; i++){
for(int j=0; j<3; j++){
cost[i][j] = LLONG_MAX;
}
}
priority_queue<pair<LL,pair<int,int>>> que;
cost[S][0] = 0;
cost[U][1] = 0;
cost[V][2] = 0;
que.push({ 0,{S,0} });
que.push({ 0,{U,1} });
que.push({ 0,{V,2} });
while(!que.empty()){
LL c = -que.top().first;
int v = que.top().second.first;
int s = que.top().second.second;
que.pop();
for(int i=0; i<edge[v].size(); i++){
int n = edge[v][i].first;
LL w = edge[v][i].second;
if(cost[n][s] > c + w){
cost[n][s] = c + w;
que.push({ -cost[n][s],{n,s} });
}
}
}
}
/*
vector<pair<LL, int>> vec;
void bfs(){
bool visited[100000] ={};
priority_queue<pair<LL, int>> que;
que.push({ cost[T][0],T });
visited[T] = true;
while(!que.empty()){
LL c = -que.top().first;
int v = que.top().second;
que.pop();
vec.PB({ c,v });
for(int i=0; i<edge[v].size(); i++){
int n = edge[v][i].first;
LL w = edge[v][i].second;
if(visited[n]) continue;
if(cost[n][0] + w == c){
visited[n] = true;
que.push({ cost[n][0],n });
}
}
}
sort(vec.begin(), vec.end());
}*/
LL dp[100000][3];
bool visited[100000] = {};
void solve(int v){
if(visited[v]) return;
visited[v] = true;
LL c = cost[v][0];
dp[v][0] = cost[v][1];
dp[v][1] = cost[v][2];
dp[v][2] = cost[v][1] + cost[v][2];
for(int i=0; i<edge[v].size(); i++){
int n = edge[v][i].first;
LL w = edge[v][i].second;
if(cost[n][0] + w == c){
solve(n);
dp[v][0] = min(dp[v][0], dp[n][0]);
dp[v][1] = min(dp[v][1], dp[n][1]);
dp[v][2] = min(dp[v][2], dp[n][2]);
dp[v][2] = min(dp[v][2], dp[n][0] + cost[v][2]);
dp[v][2] = min(dp[v][2], dp[n][1] + cost[v][1]);
}
}
}
int main(){
cin >> N >> M >> S >> T >> U >> V;
S--; T--; U--; V--;
for(int i=0; i<M; i++){
int A, B;
LL C;
cin >> A >> B >> C;
A--; B--;
edge[A].PB({ B,C });
edge[B].PB({ A,C });
}
dijkstra();
solve(T);
cout << min(cost[V][1], dp[T][2]) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}
using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;
const int inf = 1LL << 55;
const int mod = 1e9 + 7;
struct edge {
int to, cost;
edge(){}
edge(int to, int cost):to(to), cost(cost){}
};
using Graph = vector<vector<edge> >;
int N, M, S, T, U, V;
Graph graph;
vint dijkstra(int s) {
vint mincost(N, inf);
mincost[s] = 0;
priority_queue<Pi, vector<Pi>, greater<Pi> > que;
que.emplace(0, s);
while(!que.empty()) {
Pi p = que.top(); que.pop();
int v = p.second;
if(mincost[v] < p.first) continue;
for(auto e : graph[v]) {
if(mincost[v]+e.cost < mincost[e.to]) {
mincost[e.to] = mincost[v]+e.cost;
que.emplace(mincost[e.to], e.to);
}
}
}
return mincost;
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
cin >> N >> M >> S >> T >> U >> V;
--S, --T, --U, --V;
graph.resize(N);
rep(i, M) {
int a, b, c;
cin >> a >> b >> c;
--a, --b;
graph[a].emplace_back(b, c);
graph[b].emplace_back(a, c);
}
auto distS = dijkstra(S);
auto distT = dijkstra(T);
auto distU = dijkstra(U);
auto distV = dijkstra(V);
vint id(N);
iota(all(id), 0);
sort(all(id), [&](int i, int j)->bool{
return distS[i] < distS[j];
});
int ans = inf;
vint dp(N, inf);
for(int i : id) {
chmin(dp[i], distU[i]);
for(auto e : graph[i]) {
if(distS[i]+e.cost+distT[e.to] == distS[T]) {
chmin(dp[e.to], dp[i]);
}
}
}
rep(i, N) chmin(ans, dp[i]+distV[i]);
vint dp2(N, inf);
for(int i : id) {
chmin(dp2[i], distV[i]);
for(auto e : graph[i]) {
if(distS[i]+e.cost+distT[e.to] == distS[T]) {
chmin(dp2[e.to], dp2[i]);
}
}
}
rep(i, N) chmin(ans, dp2[i]+distU[i]);
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <tuple>
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using lint = long long;
constexpr lint INF = 1LL << 50;
struct Edge {
int dst;
lint cost;
explicit Edge(int dst, lint cost) : dst(dst), cost(cost) {}
};
using Graph = std::vector<std::vector<Edge>>;
std::vector<lint> dijkstra(const Graph& graph, int r) {
std::vector<lint> dist(graph.size(), INF);
dist[r] = 0;
MinHeap<std::pair<lint, int>> que;
que.emplace(dist[r], r);
while (!que.empty()) {
lint d;
int v;
std::tie(d, v) = que.top();
que.pop();
if (dist[v] < d) continue;
for (auto e : graph[v]) {
int u = e.dst;
lint nd = d + e.cost;
if (dist[u] > nd) {
dist[u] = nd;
que.emplace(nd, u);
}
}
}
return dist;
}
void solve() {
int n, m, s, t, a, b;
std::cin >> n >> m >> s >> t >> a >> b;
--s, --t, --a, --b;
Graph graph(n);
while (m--) {
int u, v;
lint c;
std::cin >> u >> v >> c;
--u, --v;
graph[u].emplace_back(v, c);
graph[v].emplace_back(u, c);
}
auto ds = dijkstra(graph, s),
da = dijkstra(graph, a),
db = dijkstra(graph, b);
std::vector<int> vs(n);
std::iota(vs.begin(), vs.end(), 0);
std::sort(vs.begin(), vs.end(),
[&](int u, int v) { return ds[u] < ds[v]; });
std::vector<std::vector<lint>>
dist(3, std::vector<lint>(n, INF));
for (auto v : vs) {
dist[0][v] = da[v];
dist[1][v] = db[v];
dist[2][v] = da[v] + db[v];
for (auto e : graph[v]) {
int u = e.dst;
if (ds[u] + e.cost > ds[v]) continue;
for (int k = 0; k < 3; ++k) {
dist[k][v] = std::min(dist[k][v], dist[k][u]);
}
}
dist[2][v] = std::min({dist[2][v],
dist[0][v] + db[v],
dist[1][v] + da[v]});
}
std::cout << std::min(da[b], dist[2][t]) << std::endl;
}
int main() {
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vll;
void Mini(ll &a,ll b){
a=min(a,b);
}
const ll INF=(1LL<<60);
struct edge{
ll to,cost;
edge(int a,int b):to(a),cost(b){}
};
int N,M,S,T,U,V;
vector< vector<edge> > G;
void dijkstra(int from,vll &dist,vll &minU,vll &distU,vll &minV,vll &distV){
for(int i=0;i<N;i++){
dist[i]=INF;
minU[i]=distU[i];
minV[i]=distV[i];
}
dist[from]=0;
priority_queue< P , vector<P> , greater<P> > Q;
Q.push(P(0,from));
while(!Q.empty()){
P p=Q.top();
Q.pop();
ll pos=p.second;
ll cost=p.first;
if(cost>dist[pos])continue;
for(int i=0;i<(int)G[pos].size();i++){
edge e=G[pos][i];
if(cost+e.cost<dist[e.to]){
dist[e.to]=cost+e.cost;
minU[e.to]=min(distU[e.to],minU[pos]);
minV[e.to]=min(distV[e.to],minV[pos]);
Q.push(P(dist[e.to],e.to));
}else if(cost+e.cost==dist[e.to]){
Mini(minU[e.to],minU[pos]);
Mini(minV[e.to],minV[pos]);
}
}
}
}
int main(){
scanf("%d %d %d %d %d %d",&N,&M,&S,&T,&U,&V);
S--;T--;U--;V--;
G.resize(N);
for(int i=0;i<M;i++){
int from,to,cost;
scanf("%d %d %d",&from,&to,&cost);
from--;to--;
G[from].push_back( edge(to,cost) );
G[to].push_back( edge(from,cost) );
}
vll distS(N),distT(N),distU(N),distV(N);
vll minSU(N),minSV(N),minTU(N),minTV(N),tmp(N);
dijkstra(U,distU,tmp,tmp,tmp,tmp);
dijkstra(V,distV,tmp,tmp,tmp,tmp);
dijkstra(S,distS,minSU,distU,minSV,distV);
dijkstra(T,distT,minTU,distU,minTV,distV);
ll ans=distU[V];
for(int i=0;i<N;i++){
if(distS[i]+distT[i]!=distS[T])continue;
Mini(ans,minSU[i]+minTV[i]);
Mini(ans,minSV[i]+minTU[i]);
}
printf("%lld\n",ans);
return 0;
}
|
//2:43
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
#define Min(a,b) a=min(a,b);
typedef long long ll;
using namespace std;
typedef pair<ll,ll> P;
#define N 100010
vector<P>g[N];
class Dijkstra{
public:
ll d[N];
void f(int root){
lol(i,N)d[i]=(ll)mod*(ll)mod;
priority_queue<P,vector<P>,greater<P> >Q;
Q.push(make_pair(0,root));
while(!Q.empty()){
ll cost=Q.top().first;
ll x=Q.top().second;
Q.pop();
if(d[x]!=(ll)mod*(ll)mod)continue;
d[x]=cost;
for(auto to:g[x]){
Q.push(make_pair(cost+to.second,to.first));
}
}
}
};
int n,m,s,t,u,v;
Dijkstra ST[2],U,V;
ll dis[2][N];
P arr[N];
int main(){
cin>>n>>m>>s>>t>>u>>v;
lol(i,m){
ll a,b,c;cin>>a>>b>>c;
g[a].push_back(make_pair(b,c));
g[b].push_back(make_pair(a,c));
}
ST[0].f(s);ST[1].f(t);U.f(u);V.f(v);
lol(k,2){
lol(i,N)arr[i]=make_pair(ST[k].d[i],i);
lol(i,N)dis[k][i]=U.d[i];
sort(arr,arr+N,greater<P>());
lol(i,N){
int x=arr[i].second;
for(auto to:g[x]){
ll y=to.first,len=to.second;
bool ok=true;
ok&=(ST[0].d[t]==ST[0].d[y]+ST[1].d[y]);
ok&=(ST[1-k].d[x]+len==ST[1-k].d[y]);
ok&=(ST[0+k].d[y]+len==ST[0+k].d[x]);
if(ok)Min(dis[k][y],dis[k][x]);
}
}
}
ll ans=U.d[v];
lol(i,N){
ll mini=min(dis[0][i],dis[1][i]);
ll maxi=max(dis[0][i],dis[1][i]);
if(maxi>=(ll)mod*(ll)mod)continue;
Min(ans,mini+V.d[i]);
}
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll>P;
vector<P>E[200000];
int s[4];
ll d[4][200000];
ll dp[200000];
int n,m;
ll solve(){
ll Min=LLONG_MAX;
memset(dp,0x3f,sizeof(dp));
dp[s[0]]=d[2][s[0]];
vector<int>v;
rep(i,n)v.push_back(i);
sort(v.begin(),v.end(),[&](int a,int b){return d[0][a]<d[0][b];});
for(int i:v){
if(d[0][i]+d[1][i]!=d[0][s[1]])continue;
dp[i]=min(dp[i],d[2][i]);
Min=min(Min,dp[i]+d[3][i]);
for(auto u:E[i]){
if(d[0][u.second]==d[0][i]+u.first){
dp[u.second]=min(dp[u.second],dp[i]);
}
}
}
return Min;
}
int main(){
cin>>n>>m;
rep(i,4)cin>>s[i],s[i]--;
rep(i,m){
int a,b,c;scanf("%d%d%d",&a,&b,&c);a--;b--;
E[a].push_back(P(c,b));
E[b].push_back(P(c,a));
}
memset(d,0x3f,sizeof(d));
rep(i,4){
priority_queue<P,vector<P>,greater<P>>que;
d[i][s[i]]=0;que.push(P(0,s[i]));
while(!que.empty()){
P p=que.top();que.pop();
if(d[i][p.second]!=p.first)continue;
for(auto u:E[p.second]){
if(d[i][u.second]>p.first+u.first){
d[i][u.second]=p.first+u.first;
que.push(P(d[i][u.second],u.second));
}
}
}
}
ll ans=min(d[2][s[3]],solve());
swap(s[2],s[3]);
rep(i,n)swap(d[2][i],d[3][i]);
ans=min(ans,solve());
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
#define int long long
#define inf 1000000007
#define pa pair<int,int>
#define ll long long
#define pal pair<double,double>
#define ppap pair<pa,int>
#define PI 3.14159265358979323846
#define paa pair<int,char>
#define mp make_pair
#define pb push_back
#define EPS (1e-8)
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,-1,1,1,-1};
using namespace std;
class pa3{
public:
int x;
int y,z;
pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {}
bool operator < (const pa3 &p) const{
if(x!=p.x) return x<p.x;
if(y!=p.y) return y<p.y;
return z<p.z;
//return x != p.x ? x<p.x: y<p.y;
}
bool operator > (const pa3 &p) const{
if(x!=p.x) return x>p.x;
if(y!=p.y) return y>p.y;
return z>p.z;
//return x != p.x ? x<p.x: y<p.y;
}
bool operator == (const pa3 &p) const{
return x==p.x && y==p.y && z==p.z;
}
bool operator != (const pa3 &p) const{
return !( x==p.x && y==p.y && z==p.z);
}
};
class pa4{
public:
int x;
int y,z,w;
pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {}
bool operator < (const pa4 &p) const{
if(x!=p.x) return x<p.x;
if(y!=p.y) return y<p.y;
if(z!=p.z)return z<p.z;
return w<p.w;
//return x != p.x ? x<p.x: y<p.y;
}
bool operator > (const pa4 &p) const{
if(x!=p.x) return x>p.x;
if(y!=p.y) return y>p.y;
if(z!=p.z)return z>p.z;
return w>p.w;
//return x != p.x ? x<p.x: y<p.y;
}
bool operator == (const pa4 &p) const{
return x==p.x && y==p.y && z==p.z &&w==p.w;
}
};
class pa2{
public:
int x,y;
pa2(int x=0,int y=0):x(x),y(y) {}
pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);}
pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);}
bool operator < (const pa2 &p) const{
return y != p.y ? y<p.y: x<p.x;
}
bool operator > (const pa2 &p) const{
return x != p.x ? x<p.x: y<p.y;
}
bool operator == (const pa2 &p) const{
return abs(x-p.x)==0 && abs(y-p.y)==0;
}
bool operator != (const pa2 &p) const{
return !(abs(x-p.x)==0 && abs(y-p.y)==0);
}
};
string itos( int i ) {
ostringstream s ;
s << i ;
return s.str() ;
}
int gcd(int v,int b){
if(v==0) return b;
if(b==0) return v;
if(v>b) return gcd(b,v);
if(v==b) return b;
if(b%v==0) return v;
return gcd(v,b%v);
}
int mod;
int extgcd(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
int d = extgcd(b, a%b, y, x);
y -= a/b * x;
return d;
}
pa operator+(const pa & l,const pa & r) {
return {l.first+r.first,l.second+r.second};
}
pa operator-(const pa & l,const pa & r) {
return {l.first-r.first,l.second-r.second};
}
int pr[10000100];
int inv[10000010];
int beki(int wa,int rr,int warukazu){
if(rr==0) return 1%warukazu;
if(rr==1) return wa%warukazu;
wa%=warukazu;
if(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu;
ll zx=beki(wa,rr/2,warukazu);
return (zx*zx)%warukazu;
}
int comb(int nn,int rr){
if(rr<0 || rr>nn || nn<0) return 0;
int r=pr[nn]*inv[rr];
r%=mod;
r*=inv[nn-rr];
r%=mod;
return r;
}
void gya(int ert){
pr[0]=1;
for(int i=1;i<=ert;i++){
pr[i]=(pr[i-1]*i)%mod;
}
inv[ert]=beki(pr[ert],mod-2,mod);
for(int i=ert-1;i>=0;i--){
inv[i]=inv[i+1]*(i+1)%mod;
}
}
// cin.tie(0);
// ios::sync_with_stdio(false);
//priority_queue<pa3,vector<pa3>,greater<pa3>> pq;
//sort(ve.begin(),ve.end(),greater<int>());
// mt19937(clock_per_sec);
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ;
vector<pa> G[100020];
int dis[100020][4];
int n;
void dij(int r,int po){
for(int i=1;i<=n;i++)dis[i][po]=-1;
priority_queue<pa,vector<pa>,greater<pa>> pq;
pq.push(mp(0,r));
while(pq.size()){
pa z=pq.top();
pq.pop();
if(dis[z.second][po]>=0) continue;
dis[z.second][po]=z.first;
for(auto v:G[z.second])pq.push(mp(z.first+v.second,v.first));
}
}
vector<int> G1[100020];
vector<int> G2[100020];
int nyu[100020]={};
int use[100020]={};
int ue[100020][2];
int si[100020][2];
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
int m;
cin>>n>>m;
int S,T,U,V;
cin>>S>>T>>U>>V;
for(int i=0;i<m;i++){
int y,yy,yyy;
cin>>y>>yy>>yyy;
G[y].pb(mp(yy,yyy));
G[yy].pb(mp(y,yyy));
}
dij(S,0);
dij(T,1);
dij(U,2);
dij(V,3);
int ST=dis[T][0];
for(int i=1;i<=n;i++)for(auto v:G[i]){
int s=i,t=v.first,val=v.second;
if(dis[s][0]+dis[t][1]+val==ST){
G1[s].pb(t);
G2[t].pb(s);
nyu[t]++;
use[s]=1,use[t]=1;
}
}
queue<int> qu;
for(int i=1;i<=n;i++)if(use[i])if(nyu[i]==0)qu.push(i);
vector<int> topo;
while(qu.size()){
int y=qu.front();
qu.pop();
topo.pb(y);
for(auto v:G1[y]){
nyu[v]--;
if(nyu[v]==0)qu.push(v);
}
}
for(auto v:topo){
ue[v][0]=dis[v][2];
ue[v][1]=dis[v][3];
for(auto r:G2[v]){
ue[v][0]=min(ue[v][0],ue[r][0]);
ue[v][1]=min(ue[v][1],ue[r][1]);
}
}
reverse(topo.begin(),topo.end());
for(auto v:topo){
si[v][0]=dis[v][2];
si[v][1]=dis[v][3];
for(auto r:G1[v]){
si[v][0]=min(si[v][0],si[r][0]);
si[v][1]=min(si[v][1],si[r][1]);
}
}
int ans=dis[U][3];
for(int i=1;i<=n;i++)if(use[i]==1){
ans=min({ans,ue[i][0]+si[i][1],si[i][0]+ue[i][1]});
}
cout<<ans<<endl;
return 0;
}
|
#include "bits/stdc++.h"
#pragma warning(disable:4996)
using namespace std;
using ld=long double;
ld eps=1e-9;
vector<vector<int>>pres;
vector<long long int>dfs(const vector<vector<pair<int, long long int>>>&edges, const int start,bool flag=false) {
vector<long long int>dp(edges.size(),1e18);
if (flag) {
pres.resize(edges.size());
}
dp[start]=0;
priority_queue<pair<long long int,int>>que;
que.emplace(0,start);
while (!que.empty()) {
auto p(que.top());
que.pop();
const int now(p.second);
const long long int now_cost(-p.first);
if(now_cost!=dp[now])continue;
for (auto e : edges[now]) {
const int next(e.first);
const long long int next_cost(now_cost+e.second);
if (dp[next] > next_cost) {
dp[next]=next_cost;
que.emplace(-next_cost,next);
if(flag)pres[next] = vector<int>{ now };
}
else if (dp[next] == next_cost) {
if(flag)pres[next].push_back(now);
}
}
}
return dp;
}
long long int solve(int N, int M, int S,int T ,int U, int V,
const vector<vector<pair<int ,long long int>>>&edges) {
vector<long long int>from_Us(N);
vector<long long int>from_Vs(N);
vector<long long int>from_Ss(N);
from_Us = dfs(edges, U);
from_Vs = dfs(edges, V);
pres.clear();
from_Ss = dfs(edges, S, true);
long long int ans = from_Us[V];
priority_queue<pair<long long int, int>>que;
que.emplace(make_pair(from_Ss[T], T));
vector<int>flags(N);
vector<long long int>mins(N, 1e18);
for (int i = 0; i < N; ++i) {
mins[i] = from_Vs[i];
}
while (!que.empty()) {
auto atop(que.top());
que.pop();
const int now = atop.second;
long long int amin = mins[now];
long long int nans = amin + from_Us[now];
ans = min(ans, nans);
for (auto e : pres[now]) {
mins[e] = min(mins[e], amin);
if (!flags[e]) {
flags[e] = true;
que.emplace(make_pair(from_Ss[e], e));
}
}
}
return ans;
}
int main() {
int N,M,S,T,U,V;cin>>N>>M>>S>>T>>U>>V;
S--;T--;U--;V--;
vector<vector<pair<int, long long int>>>edges(N);
for (int i = 0; i < M; ++i) {
int a, b, c; cin >> a >> b >> c; a--; b--;
edges[a].push_back(make_pair(b, c));
edges[b].push_back(make_pair(a, c));
}
long long int ans=solve(N,M,S,T,U,V,edges);
ans=min(ans,solve(N,M,S,T,V,U,edges));
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define int long long
#define N 100005
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
const int INF = 1e17;
int n, m, S, T, U, V;
vector<P> G[N];
int d[2][N];
P1 d2[2][N];
int dijkstra(int s, int t, int idx){
for(int i=0;i<n;i++) d[idx][i] = INF;
priority_queue<P,vector<P>,greater<P> > q;
d[idx][s] = 0;
q.push(P( 0, s ));
while(!q.empty()){
P tmp = q.top(); q.pop();
int node = tmp.second;
int cost = tmp.first;
if( d[idx][node] < cost ) continue;
for(P p : G[node] ){
int nnode = p.first;
int ncost = cost + p.second;
if( d[idx][nnode] > ncost ){
d[idx][nnode] = ncost;
q.push(P( ncost, nnode ));
}
}
}
return d[idx][t];
}
void dijkstra2(int s, int t, int idx){
for(int i=0;i<n;i++) d2[idx][i] = P1( INF, P( INF, INF) );
priority_queue<P,vector<P>,greater<P> > q;
d2[idx][s] = P1( 0, P( d[0][s], d[1][s] ) );
q.push(P( 0, s ));
while(!q.empty()){
P tmp = q.top(); q.pop();
int node = tmp.second;
int cost = tmp.first;
int u = d2[idx][node].second.first;
int v = d2[idx][node].second.second;
if( d2[idx][node].first < cost ) continue;
for(P p : G[node] ){
int nnode = p.first;
int ncost = cost + p.second;
int minu = min( u, d[0][nnode] );
int minv = min( v, d[1][nnode] );
if( d2[idx][nnode].first > ncost ){
d2[idx][nnode].first = ncost;
d2[idx][nnode].second.first = minu;
d2[idx][nnode].second.second = minv;
q.push(P( ncost, nnode ));
}
if( d2[idx][nnode].first == ncost ){
d2[idx][nnode].second.first = min( d2[idx][nnode].second.first, minu );
d2[idx][nnode].second.second = min( d2[idx][nnode].second.second, minv );
}
}
}
}
int used[N];
int ans = INF;
void dfs(int x){
if( used[x] == 1 ) return ;
used[x] = 1;
int minc = min( d2[0][x].second.first + d2[1][x].second.second,
d2[1][x].second.first + d2[0][x].second.second );
ans = min( ans, minc );
for(P p : G[x] ){
int nx = p.first;
int cost = p.second;
if( d2[0][x].first - cost == d2[0][nx].first ) dfs( nx );
}
}
void solve(){
ans = min( ans, dijkstra( U, V, 0 ) );
ans = min( ans, dijkstra( V, U, 1 ) );
dijkstra2( S, T, 0 );
dijkstra2( T, S, 1 );
dfs( T );
cout<<ans<<endl;
}
signed main(){
cin>>n>>m;
cin>>S>>T; S--, T--;
cin>>U>>V; U--, V--;
for(int i=0;i<m;i++){
int a, b, c;
cin>>a>>b>>c;
G[a-1].push_back(P( b-1, c ));
G[b-1].push_back(P( a-1, c ));
}
solve();
return 0;
}
|
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
typedef pair<int, int> pii;
typedef long long ll;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
int N, M, S, T, U, V;
using Graph = vector< vector< pair<int, int> > >;
using Queue = priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > >;
vector<int> dijkstra(Graph &G, int root) {
vector<int> dist(N, INF);
dist[root] = 0;
Queue que;
que.emplace(0, root);
while(que.size()) {
int d, pos; tie(d, pos) = que.top(); que.pop();
if(dist[pos] < d) continue;
for(auto e : G[pos]) {
int to, w; tie(to, w) = e;
if(dist[to] > d + w) {
dist[to] = d + w;
que.emplace(d + w, to);
}
}
}
return dist;
}
int solve(Graph &G, vector<int> distU, vector<int> distV) {
// dp1 := U から i まで到達するために必要なコスト (S-T 最短パスに含まれるところに関してはコストを 0 とする)
// dp2 := U から x まで進み、S-T 最短パスに含まれる x-y パスを S から遠ざかるように通って、y から V まで進むときのコスト最小
// dp3 := V から i まで到達するために必要なコスト (S-T 最短パスに含まれるところに関してはコストを 0 とする)
// dp4 := V から x まで進み、S-T 最短パスに含まれる x-y パスを S から遠ざかるように通って、y から U まで進むときのコスト最小
vector<int> dp1(N), dp2(N, INF), dp3(N), dp4(N, INF), dist(N, INF);
for(int i=0; i<N; i++) {
dp1[i] = distU[i];
dp3[i] = distV[i];
}
Queue que;
que.emplace(0, S);
dist[S] = 0;
while(que.size()) {
int d, pos; tie(d, pos) = que.top(); que.pop();
if(dist[pos] < d) continue;
for(auto e : G[pos]) {
int pre, w; tie(pre, w) = e;
// d(S, pre) + d(pre, pos) = d(S, pos) ならば、
// その辺は S-pos 最短パスに含まれる辺であるため、
// 辺のコストを無視できる
if(dist[pre] + w == dist[pos]) {
chmin(dp1[pos], dp1[pre]);
chmin(dp3[pos], dp3[pre]);
}
}
// pos から全て真面目にコストがかかったとする場合
chmin(dp2[pos], dp1[pos] + distV[pos]);
chmin(dp4[pos], dp3[pos] + distU[pos]);
for(auto e : G[pos]) {
int adj, w; tie(adj, w) = e;
// dp2, dp4 の最適値を受け渡す
// こうすることで答えが dp[T] に現れるようになる
if(dist[adj] + w == dist[pos]) {
chmin(dp2[pos], dp2[adj]);
chmin(dp4[pos], dp4[adj]);
}
if(dist[adj] > d + w) {
dist[adj] = d + w;
que.emplace(dist[adj], adj);
}
}
}
return min(dp2[T], dp4[T]);
}
signed main() {
cin >> N >> M >> S >> T >> U >> V;
S--, T--, U--, V--;
Graph G(N);
for(int i=0; i<M; i++) {
int u, v, w; scanf("%lld%lld%lld", &u, &v, &w);
u--; v--;
G[u].emplace_back(v, w);
G[v].emplace_back(u, w);
}
auto distU = dijkstra(G, U), distV = dijkstra(G, V);
cout << min(distU[V], solve(G, distU, distV)) << endl;
return 0;
}
|
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <functional>
#include <cassert>
typedef long long ll;
using namespace std;
#define debug(x) cerr << #x << " = " << (x) << endl;
#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010
int n, m, S, T, U, V;
int a[SIZE], b[SIZE], c[SIZE];
vector<pair<int, int> > G[SIZE];
vector<pair<int, int> > G2[SIZE];
vector<pair<int, int> > G2r[SIZE];
void dijkstra(vector<pair<int,int> > *G, int q, int *start, ll *res){
priority_queue<pair<pair<ll,int>,int> > pq;
bool visited[3][SIZE] = {};
for(int i=0;i<q;i++){
pq.push({{0, start[i]},0});
}
while(pq.size()){
auto p = pq.top();
pq.pop();
int now = p.first.second;
ll cost = p.first.first;
int f = p.second;
if(visited[f][now]) continue;
if(!visited[0][now] && !visited[1][now] && !visited[2][now])
res[now] = -cost;
visited[f][now] = true;
for(int i=0;i<G[now].size();i++){
if(G[now][i].first == -1){
if(f == 0 || f == 1)
pq.push({{cost, G[now][i].second},1});
}else{
if(f == 0)
pq.push({{cost - c[G[now][i].first], G[now][i].second},0});
else
pq.push({{cost - c[G[now][i].first], G[now][i].second},2});
}
}
}
}
int main(){
ll res1[SIZE], res2[SIZE];
int start[SIZE];
scanf("%d%d%d%d%d%d",&n,&m,&S,&T,&U,&V);
S--; T--; U--; V--;
for(int i=0;i<m;i++){
scanf("%d%d%d",a+i, b+i, c+i);
a[i]--; b[i]--;
G[a[i]].push_back({i, b[i]});
G[b[i]].push_back({i, a[i]});
}
start[0] = S;
for(int i=0;i<n;i++) res1[i] = -1;
dijkstra(G, 1, start, res1);
queue<int> que;
bool visited[SIZE] = {};
que.push(T);
for(int i=0;i<n;i++) G2r[i] = G[i];
for(int i=0;i<n;i++) G2[i] = G[i];
while(que.size()){
int p = que.front();
que.pop();
if(visited[p]) continue;
visited[p] = true;
for(int i=0;i<G[p].size();i++){
if(res1[p] - c[G[p][i].first] == res1[G[p][i].second]){
que.push(G[p][i].second);
G2r[p].push_back({-1, G[p][i].second});
G2[G[p][i].second].push_back({-1, p});
}
}
}
start[0] = V;
for(int i=0;i<n;i++) res1[i] = -1;
for(int i=0;i<n;i++) res2[i] = -1;
dijkstra(G2, 1, start, res1);
dijkstra(G2r, 1, start, res2);
cout << min(res1[U], res2[U]) << endl;
return 0;
}
|
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <functional>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <iostream>
#include <limits>
#include <numeric>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
struct Node {
int u;
ll step;
Node(int u, ll step) : u(u), step(step) {}
bool operator>(const Node& rhs) const { return step > rhs.step; }
};
struct Edge {
int v, w;
Edge* next;
};
const int N = 100010, M = 200010;
int n, m;
int s, t, a, b;
ll ans;
Edge* g[N];
bool vis[N];
ll diss[N], disa[N], disb[N], da[N], db[N];
void adde(int u, int v, int w);
void runspt(int s, ll* dis);
void dfs(int u);
int main() {
scanf("%d%d%d%d%d%d", &n, &m, &s, &t, &a, &b);
while (m--) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
adde(u, v, w);
adde(v, u, w);
}
runspt(a, disa);
ans = disa[b];
if (disa[s] != -1) {
runspt(b, disb);
runspt(s, diss);
dfs(t);
}
printf("%lld\n", ans);
return 0;
}
void dfs(int u) {
if (vis[u])
return;
vis[u] = true;
da[u] = disa[u];
db[u] = disb[u];
for (Edge* p = g[u]; p; p = p->next)
if (diss[p->v] + p->w == diss[u]) {
dfs(p->v);
da[u] = min(da[u], da[p->v]);
db[u] = min(db[u], db[p->v]);
}
ans = min(ans, min(da[u] + disb[u], db[u] + disa[u]));
}
void runspt(int s, ll* dis) {
memset(dis, -1, sizeof(ll) * (n + 1));
priority_queue<Node, vector<Node>, greater<Node>> q;
q.emplace(s, dis[s] = 0);
while (!q.empty()) {
Node tmp = q.top();
q.pop();
if (tmp.step != dis[tmp.u])
continue;
for (Edge* p = g[tmp.u]; p; p = p->next)
if (dis[p->v] == -1 || dis[p->v] > tmp.step + p->w)
q.emplace(p->v, dis[p->v] = tmp.step + p->w);
}
}
void adde(int u, int v, int w) {
static Edge pool[M * 2];
static Edge* p = pool;
p->v = v;
p->w = w;
p->next = g[u];
g[u] = p;
++p;
}
|
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define HUGE_NUM 99999999999999999
#define NUM 100001
enum Type{
from_S,
from_T,
from_U,
from_V,
};
struct Edge{
Edge(int arg_to,ll arg_cost){
to = arg_to;
cost = arg_cost;
}
int to;
ll cost;
};
struct Info{
Info(int arg_node_id,ll arg_sum_cost){
node_id = arg_node_id;
sum_cost = arg_sum_cost;
}
bool operator<(const struct Info & arg) const{
return sum_cost > arg.sum_cost;
}
int node_id;
ll sum_cost;
};
struct Data{
Data(int arg_node_id,ll arg_dist){
node_id = arg_node_id;
dist = arg_dist;
}
bool operator<(const struct Data &arg) const{
return dist < arg.dist;
}
int node_id;
ll dist;
};
int N,M,S,T,U,V;
int start[4];
vector<Edge> G[NUM];
ll min_dist[NUM][4],min_dist_from_U[NUM];
void calc(Type type){
for(int i = 1; i <= N; i++){
min_dist[i][type] = HUGE_NUM;
}
min_dist[start[type]][type] = 0;
priority_queue<Info> Q;
Q.push(Info(start[type],0));
int next_node;
ll next_cost;
while(!Q.empty()){
if(Q.top().sum_cost > min_dist[Q.top().node_id][type]){
Q.pop();
}else{
for(int i = 0; i < G[Q.top().node_id].size(); i++){
next_node = G[Q.top().node_id][i].to;
next_cost = Q.top().sum_cost+G[Q.top().node_id][i].cost;
if(min_dist[next_node][type] > next_cost){
min_dist[next_node][type] = next_cost;
Q.push(Info(next_node,next_cost));
}
}
Q.pop();
}
}
}
int main(){
scanf("%d %d",&N,&M);
scanf("%d %d",&S,&T);
scanf("%d %d",&U,&V);
start[from_S] = S;
start[from_T] = T;
start[from_U] = U;
start[from_V] = V;
int from,to;
ll cost;
for(int loop = 0; loop < M; loop++){
scanf("%d %d %lld",&from,&to,&cost);
G[from].push_back(Edge(to,cost));
G[to].push_back(Edge(from,cost));
}
calc(from_S);
calc(from_T);
calc(from_U);
calc(from_V);
vector<Data> DATA;
for(int i = 1; i <= N; i++){
DATA.push_back(Data(i,min_dist[i][from_S]));
}
sort(DATA.begin(),DATA.end());
ll ans = min_dist[V][from_U];
int node_id,adj_node;
for(int i = 1; i <= N; i++)min_dist_from_U[i] = min_dist[i][from_U];
for(int i = 0; i < DATA.size(); i++){
node_id = DATA[i].node_id;
ans = min(ans,min_dist[node_id][from_U]+min_dist[node_id][from_V]);
for(int k = 0; k < G[node_id].size(); k++){
adj_node = G[node_id][k].to;
if(min_dist[node_id][from_S]+G[node_id][k].cost+min_dist[adj_node][from_T] != min_dist[T][from_S])continue;
min_dist[adj_node][from_U] = min(min_dist[adj_node][from_U],min_dist[node_id][from_U]);
}
}
for(int i = 0; i < DATA.size(); i++){
node_id = DATA[i].node_id;
ans = min(ans,min_dist[node_id][from_V]+min_dist_from_U[node_id]);
for(int k = 0; k < G[node_id].size(); k++){
adj_node = G[node_id][k].to;
if(min_dist[node_id][from_S]+G[node_id][k].cost+min_dist[adj_node][from_T] != min_dist[T][from_S])continue;
min_dist[adj_node][from_V] = min(min_dist[adj_node][from_V],min_dist[node_id][from_V]);
}
}
printf("%lld\n",ans);
return 0;
}
|
#include<cmath>
#include<queue>
#include<cstdio>
#include<string>
#include<cassert>
#include<algorithm>
#define rep(i,n) for(int i=0;i<(n);i++)
#define sq(x) ((x)*(x))
#define sqsum(x,y) (sq(x)+sq(y))
using namespace std;
struct path{ int n,t[1001],x[1001],y[1001]; };
struct event{ // ロボットの接触イベント
double t;
int u,v,io; // ロボット u と v が触れた(io=0)か離れた(io=1)か
bool operator<(const event &e)const{ return t<e.t; }
};
vector<double> solve_quadratic_equation(double a,double b,double c){
if(a==0) return vector<double>(0); // a==0 は 2 つのロボットが相対的にみて動いてないということだから, このときイベントは起こらない
if(b*b-4*a*c<0) return vector<double>(0);
vector<double> res(2);
res[0]=(-b-sqrt(b*b-4*a*c))/(2*a);
res[1]=(-b+sqrt(b*b-4*a*c))/(2*a);
return res;
}
// パス P の時刻 t での位置を求める
void calc_point(const path &P,int t,int &x,int &y){
x=P.x[0];
y=P.y[0];
rep(i,P.n-1){
if(t<=P.t[i+1]){
x+=(t-P.t[i])*P.x[i+1];
y+=(t-P.t[i])*P.y[i+1];
return;
}
x+=(P.t[i+1]-P.t[i])*P.x[i+1];
y+=(P.t[i+1]-P.t[i])*P.y[i+1];
}
assert(0);
}
int R;
void calc_touch_points(const path &P,const path &Q,int u,int v,vector<event> &E){
vector<int> t;
t.insert(t.end(),P.t,P.t+P.n);
t.insert(t.end(),Q.t,Q.t+Q.n);
sort(t.begin(),t.end());
t.erase(unique(t.begin(),t.end()),t.end());
rep(k,t.size()-1){
int x1,y1,x2,y2,x3,y3,x4,y4;
calc_point(P,t[ k ],x1,y1);
calc_point(P,t[k+1],x2,y2);
calc_point(Q,t[ k ],x3,y3);
calc_point(Q,t[k+1],x4,y4);
int a=sqsum(x2-x1+x3-x4,y2-y1+y3-y4);
int b=2*(x2-x1+x3-x4)*(x1-x3)+2*(y2-y1+y3-y4)*(y1-y3);
int c=sqsum(x1-x3,y1-y3)-R*R;
vector<double> sol=solve_quadratic_equation(a,b,c);
if(sol.size()==2){
if(0<=sol[0] && sol[0]<=1) E.push_back((event){t[k]+sol[0]*(t[k+1]-t[k]),u,v,0});
if(0<=sol[1] && sol[1]<=1) E.push_back((event){t[k]+sol[1]*(t[k+1]-t[k]),u,v,1});
}
}
}
int main(){
for(int n,T;scanf("%d%d%d",&n,&T,&R),n;){
char s[100][9];
path P[100];
rep(i,n){
scanf("%s",s[i]);
int m=0,t,x,y;
while(1){
scanf("%d%d%d",&t,&x,&y);
P[i].t[m]=t;
P[i].x[m]=x;
P[i].y[m]=y;
m++;
if(t==T) break;
}
P[i].n=m;
}
vector<event> E;
rep(j,n) rep(i,j) calc_touch_points(P[i],P[j],i,j,E);
sort(E.begin(),E.end());
bool ans[100]={true};
bool adj[100][100]={}; // ロボット i と j が通信できるかどうか
// 初期状態を計算
rep(i,n) rep(j,n) if(i!=j && sqsum(P[i].x[0]-P[j].x[0],P[i].y[0]-P[j].y[0])<=R*R) {
adj[i][j]=adj[j][i]=true;
}
rep(_,n) rep(i,n) rep(j,n) if(adj[i][j]) {
ans[i]|=ans[j];
ans[j]|=ans[i];
}
// シミュレーション
queue<int> Q;
rep(i,E.size()){
event e=E[i];
int u=e.u,v=e.v;
if(e.io==0){
adj[u][v]=adj[v][u]=true;
if(ans[u]==ans[v]) continue;
// ans[u]!=ans[v] のとき
// BFS でロボット 0 の情報を広める
int tar=(ans[u]?v:u);
ans[tar]=true;
assert(Q.empty());
Q.push(tar);
while(!Q.empty()){
int w=Q.front(); Q.pop();
rep(x,n) if(adj[w][x] && !ans[x]) { ans[x]=true; Q.push(x); }
}
}
else{
adj[u][v]=adj[v][u]=false;
}
}
vector<string> s_ans;
rep(i,n) if(ans[i]) s_ans.push_back(s[i]);
sort(s_ans.begin(),s_ans.end());
rep(i,s_ans.size()) puts(s_ans[i].c_str());
}
return 0;
} |
#include<bits/stdc++.h>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-7)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define pow2(a) ((a)*(a))
using namespace std;
typedef pair<double,double> dd;
const int MAX_N = 110;
const double DINF = 1e20;
int N,T,R;
double t[MAX_N][1100];
int size[MAX_N];
double x[MAX_N][1100],y[MAX_N][1100],tmp_x[MAX_N],tmp_y[MAX_N];
double vx[MAX_N][1100],vy[MAX_N][1100];
string nickname[MAX_N];
bool knows[MAX_N];
bool LT(double a ,double b){ return !equals(a,b) && a < b; }
bool LTE(double a,double b){ return equals(a,b) || a < b; }
double getDist(double X,double Y){ return sqrt( X*X + Y*Y ); }
dd calc(double x1,double y1,double vx1,double vy1,
double x2,double y2,double vx2,double vy2,double r){
double VX = (vx1-vx2), X = (x1-x2), VY = (vy1-vy2), Y = (y1-y2);
double a = pow2(VX) + pow2(VY), b = 2*(X*VX+Y*VY), c = pow2(X) + pow2(Y) - pow2(r);
dd ret = dd(DINF,DINF);
double D = b*b - 4 * a * c;
if( LT(D,0.0) ) return ret;
if( equals(a,0.0) ) {
if( equals(b,0.0) ) return ret;
if( LT(-c/b,0.0) ) return ret;
ret.first = - c / b;
return ret;
}
if( equals(D,0.0) ) D = 0;
ret.first = ( -b - sqrt( D ) ) / ( 2 * a );
ret.second = ( -b + sqrt( D ) ) / ( 2 * a );
if( !equals(ret.first,ret.second) && ret.first > ret.second ) swap(ret.first,ret.second);
return ret;
}
const bool DEBUG = false;
int par[1000];
void init(){ rep(i,N) par[i] = i; }
int find(int x){
if( x==par[x])return par[x];
return par[x] = find(par[x]);
}
void unit(int x,int y){
x = find(x), y = find(y);
if( x != y ) par[x] = y;
}
inline void compute2(){
int cur[N];
rep(i,N) knows[i] = false, cur[i] = 0;
knows[0] = true;
double cx[N], cy[N];
rep(i,N) cx[i] = x[i][0], cy[i] = y[i][0];
init();
rep(i,N) REP(j,i+1,N) {
double dist = getDist(x[i][0]-x[j][0],y[i][0]-y[j][0]);
if( LTE(dist,R) ) unit(i,j);
}
REP(i,1,N) if( find(0) == find(i) ) knows[i] = true;
deque<double> time_stamp;
double prev = 0;
rep(i,N) rep(j,size[i]) if( t[i][j] != 0 ) time_stamp.push_back(t[i][j]);
sort(time_stamp.begin(),time_stamp.end());
time_stamp.erase(unique(time_stamp.begin(),time_stamp.end()),time_stamp.end());
while( 1 ){
double mini = 1e10;
rep(i,N) REP(j,i+1,N) {
if( cur[i]+1 >= size[i] || cur[j]+1 >= size[j] ) continue;
if( knows[i] + knows[j] != 1 ) continue;
dd res = calc(cx[i],cy[i],vx[i][cur[i]+1],vy[i][cur[i]+1],
cx[j],cy[j],vx[j][cur[j]+1],vy[j][cur[j]+1],R);
double lower = max(t[i][cur[i]],t[j][cur[j]]);
double upper = min(t[i][cur[i]+1],t[j][cur[j]+1]);
assert( LTE(lower,prev) && LTE(prev,upper) );
//cout<< "(" << res.first << "," << res.second << ")" << endl;
if( LTE(0.0,res.first) ){
if( LTE(lower,prev+res.first) && LTE(prev+res.first,upper) ) mini = min(mini,prev+res.first);
}
if( LTE(0.0,res.second) ){
if( LTE(lower,prev+res.second) && LTE(prev+res.second,upper) ) mini = min(mini,prev+res.second);
}
}
//cout << mini << endl;
bool initer = false;
if( !time_stamp.empty() && LTE(time_stamp.front(),mini) ) {
mini = time_stamp.front();
time_stamp.pop_front();
initer = true;
}
if( equals(mini,1e10) ) break;
double tm = mini;
// cout << mini << " sec " << prev << endl;
rep(j,N){
cx[j] += vx[j][cur[j]+1] * (tm-prev);
cy[j] += vy[j][cur[j]+1] * (tm-prev);
//cout << "(" << cx[j] << "," << cy[j] << ")" << endl;
}
init();
rep(j,N) if( knows[j] ) unit(0,j);
rep(j,N)REP(k,j+1,N) {
if( size[j] <= cur[j]+1 || size[k] <= cur[k]+1 ) continue;
if( !( LTE(t[j][cur[j]],tm) && LTE(tm,t[j][cur[j]+1]) ) ) continue;
if( !( LTE(t[k][cur[k]],tm) && LTE(tm,t[k][cur[k]+1]) ) ) continue;
double dist = getDist(cx[j]-cx[k],cy[j]-cy[k]);
//cout << j << " and " << k << " " << dist << endl;
if( LTE(dist,R) ) unit(j,k);
}
REP(j,1,N) if( find(0) == find(j) ) knows[j] = true;
//rep(i,N) cout << knows[i] << " "; cout << endl;
rep(i,N) if( cur[i]+1 < size[i] && equals(mini,t[i][cur[i]+1]) ) cur[i]++;
prev = mini;
//cout << "! " << mini << endl;
}
vector<string> vec;
rep(i,N) if( knows[i] ) vec.push_back(nickname[i]);
if(!vec.empty())sort(vec.begin(),vec.end());
rep(i,vec.size()) cout << vec[i] << endl;
}
int main(){
while( scanf("%d%d%d",&N,&T,&R), N|T|R ){
rep(i,N){
cin >> nickname[i];
cin >> t[i][0] >> x[i][0] >> y[i][0];
vx[i][0] = vy[i][0] = 0;
size[i] = 1;
while( cin >> t[i][size[i]] ){
cin >> vx[i][size[i]] >> vy[i][size[i]];
x[i][size[i]] = x[i][size[i]-1] + vx[i][size[i]] * ( t[i][size[i]] - t[i][size[i]-1] );
y[i][size[i]] = y[i][size[i]-1] + vy[i][size[i]] * ( t[i][size[i]] - t[i][size[i]-1] );
++size[i];
if( t[i][size[i]-1] == T ) break;
}
}
/*
rep(i,N){
cout << nickname[i] << size[i]<< " : " << endl;
rep(j,size[i]) cout << "(" << x[i][j] << "," << y[i][j] << ":" << t[i][j] << ") ";
cout << endl << endl;
}
*/
//compute();
compute2();
}
return 0;
} |
#include<bits/stdc++.h>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-7)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define pow2(a) ((a)*(a))
using namespace std;
typedef pair<double,double> dd;
const int MAX_N = 110;
const double DINF = 1e20;
int N,T,R;
double t[MAX_N][1100];
int size[MAX_N];
double x[MAX_N][1100],y[MAX_N][1100],tmp_x[MAX_N],tmp_y[MAX_N];
double vx[MAX_N][1100],vy[MAX_N][1100];
string nickname[MAX_N];
bool knows[MAX_N];
bool LT(double a ,double b){ return !equals(a,b) && a < b; }
bool LTE(double a,double b){ return equals(a,b) || a < b; }
double getDist(double X,double Y){ return sqrt( X*X + Y*Y ); }
dd calc(double x1,double y1,double vx1,double vy1,
double x2,double y2,double vx2,double vy2,double r){
double VX = (vx1-vx2), X = (x1-x2), VY = (vy1-vy2), Y = (y1-y2);
double a = pow2(VX) + pow2(VY), b = 2*(X*VX+Y*VY), c = pow2(X) + pow2(Y) - pow2(r);
dd ret = dd(DINF,DINF);
double D = b*b - 4 * a * c;
if( LT(D,0.0) ) return ret;
if( equals(a,0.0) ) {
if( equals(b,0.0) ) return ret;
if( LT(-c/b,0.0) ) return ret;
ret.first = - c / b;
return ret;
}
if( equals(D,0.0) ) D = 0;
ret.first = ( -b - sqrt( D ) ) / ( 2 * a );
ret.second = ( -b + sqrt( D ) ) / ( 2 * a );
if( !equals(ret.first,ret.second) && ret.first > ret.second ) swap(ret.first,ret.second);
return ret;
}
const bool DEBUG = false;
int par[1000];
void init(){ rep(i,N) par[i] = i; }
int find(int x){
if( x==par[x])return par[x];
return par[x] = find(par[x]);
}
void unit(int x,int y){
x = find(x), y = find(y);
if( x != y ) par[x] = y;
}
inline void compute2(){
int cur[N];
rep(i,N) knows[i] = false, cur[i] = 0;
knows[0] = true;
double cx[N], cy[N];
rep(i,N) cx[i] = x[i][0], cy[i] = y[i][0];
init();
rep(i,N) REP(j,i+1,N) {
double dist = getDist(x[i][0]-x[j][0],y[i][0]-y[j][0]);
if( LTE(dist,R) ) unit(i,j);
}
REP(i,1,N) if( find(0) == find(i) ) knows[i] = true;
deque<double> time_stamp;
double prev = 0;
rep(i,N) rep(j,size[i]) if( t[i][j] != 0 ) time_stamp.push_back(t[i][j]);
sort(time_stamp.begin(),time_stamp.end());
time_stamp.erase(unique(time_stamp.begin(),time_stamp.end()),time_stamp.end());
while( 1 ){
double mini = 1e10;
rep(i,N) REP(j,i+1,N) {
if( cur[i]+1 >= size[i] || cur[j]+1 >= size[j] ) continue;
if( knows[i] + knows[j] != 1 ) continue;
dd res = calc(cx[i],cy[i],vx[i][cur[i]+1],vy[i][cur[i]+1],
cx[j],cy[j],vx[j][cur[j]+1],vy[j][cur[j]+1],R);
double lower = max(t[i][cur[i]],t[j][cur[j]]);
double upper = min(t[i][cur[i]+1],t[j][cur[j]+1]);
assert( LTE(lower,prev) && LTE(prev,upper) );
if( LTE(0.0,res.first) ){
if( LTE(lower,prev+res.first) && LTE(prev+res.first,upper) ) mini = min(mini,prev+res.first);
}
if( LTE(0.0,res.second) ){
if( LTE(lower,prev+res.second) && LTE(prev+res.second,upper) ) mini = min(mini,prev+res.second);
}
}
bool initer = false;
if( !time_stamp.empty() && LTE(time_stamp.front(),mini) ) {
mini = time_stamp.front();
time_stamp.pop_front();
initer = true;
}
if( equals(mini,1e10) ) break;
double tm = mini;
rep(j,N){
cx[j] += vx[j][cur[j]+1] * (tm-prev);
cy[j] += vy[j][cur[j]+1] * (tm-prev);
}
init();
rep(j,N) if( knows[j] ) unit(0,j);
rep(j,N)REP(k,j+1,N) {
if( size[j] <= cur[j]+1 || size[k] <= cur[k]+1 ) continue;
if( !( LTE(t[j][cur[j]],tm) && LTE(tm,t[j][cur[j]+1]) ) ) continue;
if( !( LTE(t[k][cur[k]],tm) && LTE(tm,t[k][cur[k]+1]) ) ) continue;
double dist = getDist(cx[j]-cx[k],cy[j]-cy[k]);
if( LTE(dist,R) ) unit(j,k);
}
REP(j,1,N) if( find(0) == find(j) ) knows[j] = true;
rep(i,N) if( cur[i]+1 < size[i] && equals(mini,t[i][cur[i]+1]) ) cur[i]++;
prev = mini;
}
vector<string> vec;
rep(i,N) if( knows[i] ) vec.push_back(nickname[i]);
if(!vec.empty())sort(vec.begin(),vec.end());
rep(i,vec.size()) cout << vec[i] << endl;
}
int main(){
while( scanf("%d%d%d",&N,&T,&R), N|T|R ){
rep(i,N){
cin >> nickname[i];
cin >> t[i][0] >> x[i][0] >> y[i][0];
vx[i][0] = vy[i][0] = 0;
size[i] = 1;
while( cin >> t[i][size[i]] ){
cin >> vx[i][size[i]] >> vy[i][size[i]];
x[i][size[i]] = x[i][size[i]-1] + vx[i][size[i]] * ( t[i][size[i]] - t[i][size[i]-1] );
y[i][size[i]] = y[i][size[i]-1] + vy[i][size[i]] * ( t[i][size[i]] - t[i][size[i]-1] );
++size[i];
if( t[i][size[i]-1] == T ) break;
}
}
compute2();
}
return 0;
} |
#include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
int n, T, r;
string name[100];
vi t[100], vx[100], vy[100], x[100], y[100];
bool e[100][100], data[100][100];
inline double d2(ll x, ll y){
return x * x + y * y;
}
inline vector<double> crosspoint(ll x, ll y, ll vx, ll vy){
ll a = vx * vx + vy * vy;
ll b = x * vx + y * vy;
ll c = x * x + y * y - r * r;
vector<double> res;
if(a == 0){
if(b == 0) return res;
assert(0);
}
double D = b * b - a * c;
if(D < -EPS) return res;
D = max(0.0, D);
res.pb((-b - sqrt(D)) / a);
res.pb((-b + sqrt(D)) / a);
rep(i, 2){
double tx = x + vx * res[i], ty = y + vy * res[i];
assert(abs(hypot(tx, ty) - r) < EPS);
}
return res;
}
int main(){
while(cin >> n >> T >> r, n){
memset(e, 0, sizeof(e));
memset(data, 0, sizeof(data));
rep(i, n){
t[i].clear(); vx[i].clear(); vy[i].clear();
x[i].clear(); y[i].clear();
int a, b, c;
cin >> name[i] >> a >> b >> c;
t[i].pb(a); x[i].pb(b); y[i].pb(c);
while(cin >> a >> b >> c){
t[i].pb(a); vx[i].pb(b); vy[i].pb(c);
int dt = a - t[i][t[i].size() - 2];
x[i].pb(x[i].back() + dt * vx[i].back());
y[i].pb(y[i].back() + dt * vy[i].back());
if(a == T) break;
}
}
vector<pair<double, int> > event;
rep(i, n) rep(j, i){
if(d2(x[i][0] - x[j][0], y[i][0] - y[j][0]) <= r * r) e[i][j] = e[j][i] = 1;
rep(k, vx[i].size()) rep(l, vx[j].size()){
if(t[i][k] > t[j][l + 1] || t[j][l] > t[i][k + 1]) continue;
int dt = t[j][l] - t[i][k];
int dvx = vx[i][k] - vx[j][l], dvy = vy[i][k] - vy[j][l];
int dx = x[i][k] + vx[i][k] * dt - x[j][l], dy = y[i][k] + vy[i][k] * dt - y[j][l];
vector<double> ts = crosspoint(dx, dy, dvx, dvy);
rep(h, ts.size()){
double tt = ts[h] + t[j][l];
if(max(t[i][k], t[j][l]) <= tt && tt <= min(t[i][k + 1], t[j][l + 1])){
int tmp = i * n + j + 1;
event.pb(mp(tt, h == 0 ? tmp : -tmp));
}
}
}
}
sort(all(event));
rep(i, n) rep(j, n) data[i][j] = i == j || e[i][j];
rep(k, n) rep(i, n) rep(j, n) data[i][j] |= data[i][k] && data[k][j];
each(it, event){
int a = (abs(it->second) - 1) / n, b = (abs(it->second) - 1) % n;
if(it->second > 0){
e[a][b] = e[b][a] = 1;
bool g[100][100];
rep(i, n) rep(j, n) g[i][j] = e[i][j] || i == j;
rep(k, n) rep(i, n) rep(j, n) g[i][j] |= g[i][k] && g[k][j];
rep(i, n) rep(j, n) if(g[i][j]) rep(k, n) data[i][k] = data[j][k] = data[i][k] || data[j][k];
}
else{
e[a][b] = e[b][a] = 0;
}
}
vector<string> ans;
rep(i, n) if(data[i][0]) ans.pb(name[i]);
sort(all(ans));
rep(i, ans.size()) cout << ans[i] << endl;
}
return 0;
} |
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;
void searchAccess(const vector<vector<bool> >& graph, int start, bitset<100>& access)
{
int n = graph.size();
access.reset();
access[start] = true;
queue<int> q;
q.push(start);
while(!q.empty()){
for(int i=0; i<n; ++i){
if(graph[q.front()][i] && !access[i]){
q.push(i);
access[i] = true;
}
}
q.pop();
}
}
const double EPS = 1.0e-10;
int pow2(int a){
return a*a;
}
class Point{
public:
int y, x;
Point(int y0, int x0){
y = y0;
x = x0;
}
int dist2(const Point& p) const{
return pow2(y-p.y) + pow2(x-p.x);
}
Point operator+(const Point& p) const{
return Point(y+p.y, x+p.x);
}
Point operator-(const Point& p) const{
return Point(y-p.y, x-p.x);
}
Point operator*(int a) const{
return Point(y*a, x*a);
}
};
vector<vector<int> > test;
int main()
{
for(;;){
int n, maxT, r;
cin >> n >> maxT >> r;
if(n == 0)
return 0;
vector<string> name(n);
vector<Point> p(n, Point(0, 0));
vector<vector<int> > t(n, vector<int>(1));
vector<vector<Point> > v(n, vector<Point>(1, Point(0, 0)));
for(int i=0; i<n; ++i){
cin >> name[i] >> t[i][0] >> p[i].x >> p[i].y;
for(;;){
int t0, x0, y0;
cin >> t0 >> x0 >> y0;
t[i].push_back(t0);
v[i].push_back(Point(y0, x0));
if(t0 == maxT)
break;
}
}
vector<vector<bool> > connect(n, vector<bool>(n, false));
for(int i=0; i<n; ++i){
for(int j=i+1; j<n; ++j){
if(p[i].dist2(p[j]) <= r * r)
connect[i][j] = connect[j][i] = true;
}
}
bitset<100> check;
searchAccess(connect, 0, check);
multimap<double, pair<int, int> > mm;
for(int i=0; i<n; ++i){
for(int j=i+1; j<n; ++j){
Point p1 = p[i];
Point p2 = p[j];
unsigned k1 = 1;
unsigned k2 = 1;
double prevT = -1.0;
while(k1 < v[i].size() && k2 < v[j].size()){
Point v1 = v[i][k1];
Point v2 = v[j][k2];
Point dp = (p1 - v1 * t[i][k1-1]) - (p2 - v2 * t[j][k2-1]);
Point dv = v1 - v2;
int a = pow2(dv.y) + pow2(dv.x);
int b = 2 * (dp.y * dv.y + dp.x * dv.x);
int c = pow2(dp.y) + pow2(dp.x) - pow2(r);
int d = pow2(b) - 4 * a * c;
if(a > 0 && d >= 0){
double t1 = (-b - sqrt((double)d)) / (2 * a);
double t2 = (-b + sqrt((double)d)) / (2 * a);
if(max(t[i][k1-1], t[j][k2-1]) <= t1 + EPS && t1 - EPS <= min(t[i][k1], t[j][k2]) && abs(prevT - t1) > EPS){
mm.insert(make_pair(t1, make_pair(i, j)));
prevT = t1;
}
if(max(t[i][k1-1], t[j][k2-1]) <= t2 + EPS && t2 - EPS <= min(t[i][k1], t[j][k2]) && abs(prevT - t2) > EPS){
mm.insert(make_pair(t2, make_pair(i, j)));
prevT = t2;
}
}
if(t[i][k1] <= t[j][k2]){
p1 = p1 + v1 * (t[i][k1] - t[i][k1-1]);
++ k1;
}else{
p2 = p2 + v2 * (t[j][k2] - t[j][k2-1]);
++ k2;
}
}
}
}
while(!mm.empty()){
double t0 = mm.begin()->first;
int a = mm.begin()->second.first;
int b = mm.begin()->second.second;
mm.erase(mm.begin());
connect[a][b] = connect[b][a] = !connect[a][b];
if(check[a] != check[b]){
bitset<100> bs;
searchAccess(connect, a, bs);
check |= bs;
}
}
vector<string> ret;
for(int i=0; i<n; ++i){
if(check[i])
ret.push_back(name[i]);
}
sort(ret.begin(), ret.end());
for(unsigned i=0; i<ret.size(); ++i)
cout << ret[i] << endl;
}
} |
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <complex>
#include <vector>
#include <assert.h>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;
typedef complex<double> Point;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
struct Line : public vector<Point> {
Line(Point a, Point b) { push_back(a), push_back(b); }
};
struct Circle {
Point p;
double r;
Circle() {;}
Circle(Point p, double r) : p(p), r(r) {;}
};
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)
#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())
inline double cross(Point a, Point b) {
return imag(conj(a) * b);
}
inline double dot(Point a, Point b) {
return real(conj(a) * b);
}
int ccw(Point a, Point b, Point c) {
b -= a; c -= a;
if (cross(b, c) > 0) { return 1; }
if (cross(b, c) < 0) { return -1; }
if (dot(b, c) < 0) { return 2; }
if (norm(b) < norm(c)) { return -2; }
return 0;
}
bool intersectLP(const Line &l, const Point &p) {
return abs(cross(l[1] - p, l[0] - p)) < EPS;
}
bool intersectSP(const Line &s, const Point &p) {
return abs(s[0] - p) + abs(s[1] - p) - abs(s[0] - s[1]) < EPS;
}
bool intersectCP(const Circle &c, const Point &p) {
return abs(c.p - p) - c.r < EPS;
}
Point projection(const Line &l, const Point &p) {
double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
return l[0] + t * (l[0] - l[1]);
}
double distanceLP(const Line &l, const Point &p) {
return abs(p - projection(l, p));
}
double distanceSP(const Line &s, const Point &p) {
const Point r = projection(s, p);
if (intersectSP(s, r)) { return abs(r - p); }
return min(abs(s[0] - p), abs(s[1] - p));
}
double distancePP(const Point &a, const Point &b) {
return abs(a - b);
}
//include inside
bool intersectSC(const Line &s, const Circle &c) {
return distanceSP(s, c.p) <= c.r;
}
Point crosspointSC(const Line &s, const Circle &c) {
assert(intersectSC(s, c));
assert(!intersectCP(c, s[0]));
double d = distanceLP(s, c.p);
Point vect = (s[1] - s[0]);
vect /= abs(vect);
Point center1 = c.p + vect * Point(0, 1) * d;
Point center2 = c.p - vect * Point(0, 1) * d;
//cout << d << endl;
//cout << vect << endl;
//cout << center1 << " " << center2 << endl;
if (intersectLP(s, center1)) {
return center1 - vect * sqrt(c.r*c.r - d*d);
}
if (intersectLP(s, center2)) {
return center2 + vect * sqrt(c.r*c.r - d*d);
}
assert(false);
return Point(1/0.0, 1/0.0);
}
struct Node {
double t;
int robot;
Node(double t, int robot) :t(t), robot(robot) {;}
bool operator<(const Node &rhs) const { return t > rhs.t; }
};
int N, T, R;
char name[110][30];
Point inipos[110];
vector<Point> velocity[110];
vector<double> starttime[110];
bool havedata[110];
double senddata(int frobot, int trobot, double start) {
Point pos = inipos[frobot] - inipos[trobot];
Circle circle = Circle(Point(0, 0), R);
Point v;
int findex = 0;
int tindex = 0;
double ptime = 0;
while (true) {
double ntime = min(starttime[frobot][findex], starttime[trobot][tindex]);
ntime = min(ntime, start);
v = velocity[frobot][findex] - velocity[trobot][tindex];
pos += v * (ntime - ptime);
ptime = ntime;
if (ntime == start) { break; }
if (starttime[frobot][findex] < starttime[trobot][tindex]) {
findex++;
} else {
tindex++;
}
}
if (intersectCP(circle, pos)) { return start; }
while (findex != (int)starttime[frobot].size() - 1 || tindex != (int)starttime[trobot].size() - 1) {
double ntime = min(starttime[frobot][findex], starttime[trobot][tindex]);
v = velocity[frobot][findex] - velocity[trobot][tindex];
Point npos = pos + v * (ntime - ptime);
//cout << pos << " " << npos << endl;
if (intersectSC(Line(pos, npos), circle)) {
//cout << endl;
Point intersect = crosspointSC(Line(pos, npos), circle);
if (abs(v) == 0) { return ptime; }
return ptime + abs(intersect - pos) / abs(v);
}
pos = npos;
ptime = ntime;
if (starttime[frobot][findex] < starttime[trobot][tindex]) {
findex++;
} else {
tindex++;
}
}
//cout << endl;
return T + 1;
}
int main() {
while (scanf("%d %d %d", &N, &T, &R), N|T|R) {
MEMSET(havedata, false);
REP(i, N) {
velocity[i].clear();
starttime[i].clear();
scanf("%s", name[i]);
double t, x, y;
scanf("%lf %lf %lf", &t, &x, &y);
inipos[i] = Point(x, y);
velocity[i].push_back(Point(0, 0));
starttime[i].push_back(0);
while (true) {
scanf("%lf %lf %lf", &t, &x, &y);
velocity[i].push_back(Point(x, y));
starttime[i].push_back(t);
if (t == T) { break; }
}
velocity[i].push_back(Point(0, 0));
starttime[i].push_back(T + 1);
}
priority_queue<Node> que;
que.push(Node(0.0, 0));
while (!que.empty()) {
double t = que.top().t;
int robot = que.top().robot;
que.pop();
if (havedata[robot]) { continue; }
havedata[robot] = true;
//cout << robot << " " << t << endl;
REP(i, N) {
if (havedata[i]) { continue; }
double sendtime = senddata(robot, i, t);
if (sendtime <= T + EPS) {
que.push(Node(sendtime, i));
}
}
}
vector<string> ans;
REP(i, N) {
if (havedata[i]) { ans.push_back(name[i]); }
}
sort(ans.begin(), ans.end());
REP(i, ans.size()) {
cout << ans[i] << endl;
}
}
} |
#include<bits/stdc++.h>
using namespace std;
using F = double;
struct Robot {
string nickname;
vector<F> t, x, y, vx, vy;
};
int N, T, R;
vector<Robot> robot;
const F EPS = 1e-6;
vector<F> solve(F a, F b, F c) {
if(a == 0) {
if(b == 0) return {};
else return {-c / b};
}
F D = b * b - 4 * a * c;
if(D < 0) return {};
else return {(-b - sqrt(D)) / (2 * a), (-b + sqrt(D)) / (2 * a)};
}
set<F> schedule() {
set<F> res;
for(auto r: robot) for(auto t: r.t) res.emplace(t);
for(auto i = 0; i < N; ++i) for(auto j = 0; j < i; ++j) {
auto& lhs = robot[i];
auto& rhs = robot[j];
for(auto a = 0; a + 1 < lhs.t.size(); ++a) for(auto b = 0; b + 1 < rhs.t.size(); ++b) {
auto lx = lhs.x[a] - lhs.t[a] * lhs.vx[a];
auto rx = rhs.x[b] - rhs.t[b] * rhs.vx[b];
auto dx = lx - rx;
auto ly = lhs.y[a] - lhs.t[a] * lhs.vy[a];
auto ry = rhs.y[b] - rhs.t[b] * rhs.vy[b];
auto dy = ly - ry;
auto dvx = lhs.vx[a] - rhs.vx[b];
auto dvy = lhs.vy[a] - rhs.vy[b];
auto A = dvx * dvx + dvy * dvy;
auto B = 2 * dx * dvx + 2 * dy * dvy;
auto C = dx * dx + dy * dy - R * R;
auto low = max(lhs.t[a], rhs.t[b]);
auto high = min(lhs.t[a + 1], rhs.t[b + 1]);
for(auto t: solve(A, B, C)) if(low <= t + EPS && t <= high + EPS) res.emplace(t);
}
}
return res;
}
multiset<string> solve() {
vector<bool> ok(N);
ok[0] = true;
for(auto t: schedule()) {
for(auto i = 0; i < N; ++i) for(auto j = 0; j < i; ++j) {
if(!ok[i] && !ok[j]) continue;
auto& lhs = robot[i];
auto& rhs = robot[j];
auto a = 0, b = 0;
for(a = 0; a + 1 < lhs.t.size(); ++a) if(lhs.t[a] <= t + EPS && t <= lhs.t[a + 1] + EPS) break;
for(b = 0; b + 1 < rhs.t.size(); ++b) if(rhs.t[b] <= t + EPS && t <= rhs.t[b + 1] + EPS) break;
auto lx = lhs.x[a] + (t - lhs.t[a]) * lhs.vx[a];
auto rx = rhs.x[b] + (t - rhs.t[b]) * rhs.vx[b];
auto dx = lx - rx;
auto ly = lhs.y[a] + (t - lhs.t[a]) * lhs.vy[a];
auto ry = rhs.y[b] + (t - rhs.t[b]) * rhs.vy[b];
auto dy = ly - ry;
if(dx * dx + dy * dy <= R * R + EPS) ok[i] = ok[j] = true;
}
}
multiset<string> res;
for(auto i = 0; i < N; ++i) if(ok[i]) res.emplace(robot[i].nickname);
return res;
}
int main() {
while(cin >> N >> T >> R, N | T | R) {
robot = vector<Robot>(N);
for(auto& r: robot) {
cin >> r.nickname;
while(true) {
int t, vx, vy;
cin >> t >> vx >> vy;
if(r.t.empty()) {
r.x.emplace_back(vx);
r.y.emplace_back(vy);
} else {
r.vx.emplace_back(vx);
r.vy.emplace_back(vy);
auto d = t - r.t.back();
r.x.emplace_back(r.x.back() + d * vx);
r.y.emplace_back(r.y.back() + d * vy);
}
r.t.emplace_back(t);
if(t == T) break;
}
}
for(auto s: solve()) cout << s << endl;
}
} |
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <complex>
#include <vector>
#include <assert.h>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;
typedef complex<double> Point;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
struct Line : public vector<Point> {
Line(Point a, Point b) { push_back(a), push_back(b); }
};
struct Circle {
Point p;
double r;
Circle() {;}
Circle(Point p, double r) : p(p), r(r) {;}
};
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)
#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())
inline double cross(const Point &a, const Point &b) {
return imag(conj(a) * b);
}
inline double dot(const Point &a, const Point &b) {
return real(conj(a) * b);
}
int ccw(Point a, Point b, Point c) {
b -= a; c -= a;
if (cross(b, c) > 0) { return 1; }
if (cross(b, c) < 0) { return -1; }
if (dot(b, c) < 0) { return 2; }
if (norm(b) < norm(c)) { return -2; }
return 0;
}
inline bool intersectLP(const Line &l, const Point &p) {
return abs(cross(l[1] - p, l[0] - p)) < EPS;
}
inline bool intersectSP(const Line &s, const Point &p) {
return abs(s[0] - p) + abs(s[1] - p) - abs(s[0] - s[1]) < EPS;
}
inline bool intersectCP(const Circle &c, const Point &p) {
return abs(c.p - p) - c.r < EPS;
}
inline Point projection(const Line &l, const Point &p) {
double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
return l[0] + t * (l[0] - l[1]);
}
inline double distanceLP(const Line &l, const Point &p) {
return abs(p - projection(l, p));
}
inline double distanceSP(const Line &s, const Point &p) {
const Point r = projection(s, p);
if (intersectSP(s, r)) { return abs(r - p); }
return min(abs(s[0] - p), abs(s[1] - p));
}
//include inside
inline bool intersectSC(const Line &s, const Circle &c) {
return distanceSP(s, c.p) <= c.r;
}
Point crosspointSC(const Line &s, const Circle &c) {
assert(intersectSC(s, c));
assert(!intersectCP(c, s[0]));
double d = distanceLP(s, c.p);
Point vect = (s[1] - s[0]);
vect /= abs(vect);
Point center1 = c.p + vect * Point(0, 1) * d;
Point center2 = c.p - vect * Point(0, 1) * d;
//cout << d << endl;
//cout << vect << endl;
//cout << center1 << " " << center2 << endl;
if (intersectLP(s, center1)) {
return center1 - vect * sqrt(c.r*c.r - d*d);
}
return center2 - vect * sqrt(c.r*c.r - d*d);
}
struct Node {
double t;
int robot;
Node(double t, int robot) :t(t), robot(robot) {;}
bool operator<(const Node &rhs) const { return t > rhs.t; }
};
int N, T, R;
char name[110][30];
Point inipos[110];
vector<Point> velocity[110];
vector<double> starttime[110];
bool havedata[110];
double gettime[110];
double senddata(int frobot, int trobot, double start) {
Point pos = inipos[frobot] - inipos[trobot];
Circle circle = Circle(Point(0, 0), R);
Point v;
int findex = 0;
int tindex = 0;
double ptime = 0;
while (true) {
double ntime = min(starttime[frobot][findex], starttime[trobot][tindex]);
ntime = min(ntime, start);
v = velocity[frobot][findex] - velocity[trobot][tindex];
pos += v * (ntime - ptime);
ptime = ntime;
if (ntime == start) { break; }
if (starttime[frobot][findex] < starttime[trobot][tindex]) {
findex++;
} else {
tindex++;
}
}
if (intersectCP(circle, pos)) { return start; }
while (findex != (int)starttime[frobot].size() - 1 || tindex != (int)starttime[trobot].size() - 1) {
if (ptime > gettime[trobot]) { break; }
if (abs(pos) > R + (T - ptime) * 20 + 1) { break; }
double ntime = min(starttime[frobot][findex], starttime[trobot][tindex]);
v = velocity[frobot][findex] - velocity[trobot][tindex];
Point npos = pos + v * (ntime - ptime);
//cout << ntime << " " << ptime << " " << pos << " " << npos << endl;
if (intersectSC(Line(pos, npos), circle)) {
//cout << endl;
Point intersect = crosspointSC(Line(pos, npos), circle);
if (abs(v) == 0) { return ptime; }
//cout << intersect << endl;
//cout << ptime + abs(intersect - pos) / abs(v);
double ret = ptime + abs(intersect - pos) / abs(v);
assert(ptime - EPS < ret && ret < ntime + EPS);
return ret;
}
pos = npos;
ptime = ntime;
if (starttime[frobot][findex] < starttime[trobot][tindex]) {
findex++;
} else {
tindex++;
}
}
//cout << endl;
return T + 1;
}
int main() {
while (scanf("%d %d %d", &N, &T, &R), N|T|R) {
MEMSET(havedata, false);
REP(i, N) {
velocity[i].clear();
starttime[i].clear();
gettime[i] = T + EPS;
scanf("%s", name[i]);
double t, x, y;
scanf("%lf %lf %lf", &t, &x, &y);
inipos[i] = Point(x, y);
velocity[i].push_back(Point(0, 0));
starttime[i].push_back(0);
while (true) {
scanf("%lf %lf %lf", &t, &x, &y);
velocity[i].push_back(Point(x, y));
starttime[i].push_back(t);
if (t == T) { break; }
}
velocity[i].push_back(Point(0, 0));
starttime[i].push_back(T + 1);
}
priority_queue<Node> que;
que.push(Node(0.0, 0));
while (!que.empty()) {
double t = que.top().t;
int robot = que.top().robot;
que.pop();
if (havedata[robot]) { continue; }
havedata[robot] = true;
gettime[robot] = t;
//cout << robot << " " << t << endl;
REP(i, N) {
if (havedata[i]) { continue; }
double sendtime = senddata(robot, i, t);
if (sendtime < gettime[i]) {
gettime[i] = sendtime;
que.push(Node(sendtime, i));
}
}
}
vector<string> ans;
REP(i, N) {
if (havedata[i]) { ans.push_back(name[i]); }
}
sort(ans.begin(), ans.end());
REP(i, ans.size()) {
printf("%s\n", ans[i].c_str());
}
}
} |
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
//#define EPS 0.000000001
using namespace std;
#define EPS 0.000000001
#define NUM 105
struct Point{
Point(){
x = y = 0;
}
Point(int arg_x,int arg_y){
x = arg_x;
y = arg_y;
}
ll x,y;
};
typedef Point Vector;
struct Data{
Data(ll arg_Time,ll arg_Vx,ll arg_Vy){
Time = arg_Time;
Vx = arg_Vx;
Vy = arg_Vy;
}
ll Time,Vx,Vy;
};
struct Info{
Point pos_First;
string name;
vector<Data> V;
};
struct State{
State(int arg_robo_A,int arg_robo_B,double arg_Time){
robo_A = arg_robo_A;
robo_B = arg_robo_B;
Time = arg_Time;
}
bool operator<(const struct State &arg) const{
return Time > arg.Time; //時刻の昇順(PQ)
}
int robo_A,robo_B;
double Time;
};
int N;
ll T,R;
bool is_adj[NUM][NUM],is_Same[NUM];
Info info[NUM];
priority_queue<State> Q;
int calc_dist(Point A,Point B){
return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);
}
void calc_Cross_Time(int robo_A,int robo_B){
Point pos_A = info[robo_A].pos_First;
Point pos_B = info[robo_B].pos_First;
Vector v_A = Vector(info[robo_A].V[0].Vx,info[robo_A].V[0].Vy);
Vector v_B = Vector(info[robo_B].V[0].Vx,info[robo_B].V[0].Vy);
Vector next_v_A,next_v_B;
int index_A = 0,index_B = 0;
ll current_Time = 0,next_Time;
while(true){
next_v_A = v_A;
next_v_B = v_B;
//次のイベントはどちらが早いか、または同時か
if( (index_A < info[robo_A].V.size()-1 && info[robo_A].V[index_A].Time < info[robo_B].V[index_B].Time)){ //Aが早い
next_Time = info[robo_A].V[index_A].Time;
next_v_A.x = info[robo_A].V[index_A+1].Vx;
next_v_A.y = info[robo_A].V[index_A+1].Vy;
index_A++;
}else if((index_B < info[robo_B].V.size()-1 && info[robo_B].V[index_B].Time < info[robo_A].V[index_A].Time)){ //Bが早い
next_Time = info[robo_B].V[index_B].Time;
next_v_B.x = info[robo_B].V[index_B+1].Vx;
next_v_B.y = info[robo_B].V[index_B+1].Vy;
index_B++;
}else{ //同時
next_Time = info[robo_A].V[index_A].Time;
if(index_A+1 <= info[robo_A].V.size()-1){
next_v_A.x = info[robo_A].V[index_A+1].Vx;
next_v_A.y = info[robo_A].V[index_A+1].Vy;
index_A++;
}
if(index_B+1 <= info[robo_B].V.size()-1){
next_v_B.x = info[robo_B].V[index_B+1].Vx;
next_v_B.y = info[robo_B].V[index_B+1].Vy;
index_B++;
}
}
//判別式
ll a = (v_A.x-v_B.x)*(v_A.x-v_B.x)+(v_A.y-v_B.y)*(v_A.y-v_B.y);
ll b = 2*((pos_A.x-pos_B.x)*(v_A.x-v_B.x)+(pos_A.y-pos_B.y)*(v_A.y-v_B.y));
ll c = (pos_A.x-pos_B.x)*(pos_A.x-pos_B.x)+(pos_A.y-pos_B.y)*(pos_A.y-pos_B.y)-R*R;
ll D = b*b-4*a*c;
if(a != 0 && D >= 0){ //解があるならそれを求める:current_Time-next_Timeの間にあれば、それをQにpush
double A = a,B = b, C = c;
double CURRENT = current_Time,NEXT = next_Time;
double t_1 = CURRENT+(-B-sqrt(B*B-4*A*C))/(2*A);
double t_2 = CURRENT+(-B+sqrt(B*B-4*A*C))/(2*A);
if(t_1 > CURRENT+EPS && t_1+EPS < NEXT){
Q.push(State(robo_A,robo_B,t_1));
}
if(fabs(t_1-t_2) > EPS && t_2 > CURRENT+EPS && t_2+EPS < NEXT){
Q.push(State(robo_A,robo_B,t_2));
}
}
if(index_A == info[robo_A].V.size()-1 && index_B == info[robo_B].V.size()-1){
break;
}
pos_A.x += v_A.x*(next_Time-current_Time);
pos_A.y += v_A.y*(next_Time-current_Time);
pos_B.x += v_B.x*(next_Time-current_Time);
pos_B.y += v_B.y*(next_Time-current_Time);
current_Time = next_Time;
v_A = next_v_A;
v_B = next_v_B;
}
}
void func(){
for(int i = 0; i < NUM; i++){
info[i].V.clear();
}
int base_robot = 0;
ll Time,Vx,Vy;
for(int i = 0; i < N; i++){
cin >> info[i].name;
scanf("%lld %lld %lld",&Time,&info[i].pos_First.x,&info[i].pos_First.y);
while(true){
scanf("%lld %lld %lld",&Time,&Vx,&Vy);
info[i].V.push_back(Data(Time,Vx,Vy));
if(Time == T)break;
}
}
is_Same[base_robot] = true;
for(int i = 1; i < N; i++){
is_Same[i] = false;
}
for(int i = 0; i < N; i++){
for(int k = 0; k < N; k++){
is_adj[i][k] = (i==k);
}
}
//最初の位置関係
for(int a = 0; a < N-1; a++){
for(int b = a+1; b < N; b++){
//最初の距離
ll first_dist = calc_dist(info[a].pos_First,info[b].pos_First);
if(first_dist <= R*R){
is_adj[a][b] = true;
is_adj[b][a] = true;
}
}
}
for(int mid = 0; mid < N; mid++){
for(int start = 0; start < N; start++){
if(!is_adj[start][mid])continue;
for(int goal = 0; goal < N; goal++){
if(!is_adj[mid][goal])continue;
is_Same[goal] = is_Same[start]|is_Same[mid];
}
}
}
for(int a = 0; a < N-1; a++){
for(int b = a+1; b < N; b++){
calc_Cross_Time(a,b);
}
}
while(!Q.empty()){
if(is_adj[Q.top().robo_A][Q.top().robo_B]){
is_adj[Q.top().robo_A][Q.top().robo_B] = false;
is_adj[Q.top().robo_B][Q.top().robo_A] = false;
}else{
is_adj[Q.top().robo_A][Q.top().robo_B] = true;
is_adj[Q.top().robo_B][Q.top().robo_A] = true;
if(is_Same[Q.top().robo_A] == is_Same[Q.top().robo_B]){
Q.pop();
continue;
}
int robo_id;
if(is_Same[Q.top().robo_A]){
robo_id = Q.top().robo_B;
}else{
robo_id = Q.top().robo_A;
}
is_Same[robo_id] = true;
queue<int> calc_Q;
calc_Q.push(robo_id);
while(!calc_Q.empty()){
for(int i = 0; i < N; i++){
if(is_adj[calc_Q.front()][i] == true && is_Same[i] ==false){
is_Same[i] = true;
calc_Q.push(i);
}
}
calc_Q.pop();
}
}
Q.pop();
}
vector<string> ans;
for(int i = 0; i < N; i++){
if(is_Same[i]){
ans.push_back(info[i].name);
}
}
sort(ans.begin(),ans.end());
for(int i = 0; i < ans.size(); i++){
printf("%s\n",ans[i].c_str());
}
}
int main(){
while(true){
scanf("%d %lld %lld",&N,&T,&R);
if(N == 0 && T == 0 && R == 0)break;
func();
}
return 0;
}
|
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <complex>
#include <vector>
#include <assert.h>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;
typedef complex<double> Point;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
struct Line : public vector<Point> {
Line(Point a, Point b) { push_back(a), push_back(b); }
};
struct Circle {
Point p;
double r;
Circle() {;}
Circle(Point p, double r) : p(p), r(r) {;}
};
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define DEC(i, s) for (int i = (s); i >= 0; i--)
#define SIZE(v) (int)((v).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define FIND(m, w) ((m).find(w) != (m).end())
inline double cross(Point a, Point b) {
return imag(conj(a) * b);
}
inline double dot(Point a, Point b) {
return real(conj(a) * b);
}
int ccw(Point a, Point b, Point c) {
b -= a; c -= a;
if (cross(b, c) > 0) { return 1; }
if (cross(b, c) < 0) { return -1; }
if (dot(b, c) < 0) { return 2; }
if (norm(b) < norm(c)) { return -2; }
return 0;
}
bool intersectLP(const Line &l, const Point &p) {
return abs(cross(l[1] - p, l[0] - p)) < EPS;
}
bool intersectSP(const Line &s, const Point &p) {
return abs(s[0] - p) + abs(s[1] - p) - abs(s[0] - s[1]) < EPS;
}
bool intersectCP(const Circle &c, const Point &p) {
return abs(c.p - p) - c.r < EPS;
}
Point projection(const Line &l, const Point &p) {
double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
return l[0] + t * (l[0] - l[1]);
}
double distanceLP(const Line &l, const Point &p) {
return abs(p - projection(l, p));
}
double distanceSP(const Line &s, const Point &p) {
const Point r = projection(s, p);
if (intersectSP(s, r)) { return abs(r - p); }
return min(abs(s[0] - p), abs(s[1] - p));
}
double distancePP(const Point &a, const Point &b) {
return abs(a - b);
}
//include inside
bool intersectSC(const Line &s, const Circle &c) {
return distanceSP(s, c.p) <= c.r;
}
Point crosspointSC(const Line &s, const Circle &c) {
assert(intersectSC(s, c));
assert(!intersectCP(c, s[0]));
double d = distanceLP(s, c.p);
Point vect = (s[1] - s[0]);
vect /= abs(vect);
Point center1 = c.p + vect * Point(0, 1) * d;
Point center2 = c.p - vect * Point(0, 1) * d;
//cout << d << endl;
//cout << vect << endl;
//cout << center1 << " " << center2 << endl;
if (intersectLP(s, center1)) {
return center1 - vect * sqrt(c.r*c.r - d*d);
}
if (intersectLP(s, center2)) {
return center2 - vect * sqrt(c.r*c.r - d*d);
}
assert(false);
return Point(1/0.0, 1/0.0);
}
struct Node {
double t;
int robot;
Node(double t, int robot) :t(t), robot(robot) {;}
bool operator<(const Node &rhs) const { return t > rhs.t; }
};
int N, T, R;
char name[110][30];
Point inipos[110];
vector<Point> velocity[110];
vector<double> starttime[110];
bool havedata[110];
double senddata(int frobot, int trobot, double start) {
Point pos = inipos[frobot] - inipos[trobot];
Circle circle = Circle(Point(0, 0), R);
Point v;
int findex = 0;
int tindex = 0;
double ptime = 0;
while (true) {
double ntime = min(starttime[frobot][findex], starttime[trobot][tindex]);
ntime = min(ntime, start);
v = velocity[frobot][findex] - velocity[trobot][tindex];
pos += v * (ntime - ptime);
ptime = ntime;
if (ntime == start) { break; }
if (starttime[frobot][findex] < starttime[trobot][tindex]) {
findex++;
} else {
tindex++;
}
}
if (intersectCP(circle, pos)) { return start; }
while (findex != (int)starttime[frobot].size() - 1 || tindex != (int)starttime[trobot].size() - 1) {
double ntime = min(starttime[frobot][findex], starttime[trobot][tindex]);
v = velocity[frobot][findex] - velocity[trobot][tindex];
Point npos = pos + v * (ntime - ptime);
//cout << ntime << " " << ptime << " " << pos << " " << npos << endl;
if (intersectSC(Line(pos, npos), circle)) {
//cout << endl;
Point intersect = crosspointSC(Line(pos, npos), circle);
if (abs(v) == 0) { return ptime; }
//cout << intersect << endl;
//cout << ptime + abs(intersect - pos) / abs(v);
double ret = ptime + abs(intersect - pos) / abs(v);
assert(ptime - EPS < ret && ret < ntime + EPS);
return ret;
}
pos = npos;
ptime = ntime;
if (starttime[frobot][findex] < starttime[trobot][tindex]) {
findex++;
} else {
tindex++;
}
}
//cout << endl;
return T + 1;
}
int main() {
while (scanf("%d %d %d", &N, &T, &R), N|T|R) {
MEMSET(havedata, false);
REP(i, N) {
velocity[i].clear();
starttime[i].clear();
scanf("%s", name[i]);
double t, x, y;
scanf("%lf %lf %lf", &t, &x, &y);
inipos[i] = Point(x, y);
velocity[i].push_back(Point(0, 0));
starttime[i].push_back(0);
while (true) {
scanf("%lf %lf %lf", &t, &x, &y);
velocity[i].push_back(Point(x, y));
starttime[i].push_back(t);
if (t == T) { break; }
}
velocity[i].push_back(Point(0, 0));
starttime[i].push_back(T + 1);
}
priority_queue<Node> que;
que.push(Node(0.0, 0));
while (!que.empty()) {
double t = que.top().t;
int robot = que.top().robot;
que.pop();
if (havedata[robot]) { continue; }
havedata[robot] = true;
//cout << robot << " " << t << endl;
REP(i, N) {
if (havedata[i]) { continue; }
double sendtime = senddata(robot, i, t);
if (sendtime <= T + EPS) {
que.push(Node(sendtime, i));
}
}
}
vector<string> ans;
REP(i, N) {
if (havedata[i]) { ans.push_back(name[i]); }
}
sort(ans.begin(), ans.end());
REP(i, ans.size()) {
cout << ans[i] << endl;
}
}
} |
#include<bits/stdc++.h>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-7)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define pow2(a) ((a)*(a))
using namespace std;
typedef pair<double,double> dd;
const int MAX_N = 110;
const double DINF = 1e20;
int N,T,R;
int size[MAX_N];
double x[MAX_N][1100],y[MAX_N][1100],tmp_x[MAX_N],tmp_y[MAX_N],vx[MAX_N][1100],vy[MAX_N][1100],t[MAX_N][1100];
string nickname[MAX_N];
bool knows[MAX_N];
bool LT(double a ,double b){ return !equals(a,b) && a < b; }
bool LTE(double a,double b){ return equals(a,b) || a < b; }
double getDist(double X,double Y){ return sqrt( X*X + Y*Y ); }
dd calc(double x1,double y1,double vx1,double vy1,
double x2,double y2,double vx2,double vy2,double r){
double VX = (vx1-vx2), X = (x1-x2), VY = (vy1-vy2), Y = (y1-y2);
double a = pow2(VX) + pow2(VY), b = 2*(X*VX+Y*VY), c = pow2(X) + pow2(Y) - pow2(r);
dd ret = dd(DINF,DINF);
double D = b*b - 4 * a * c;
if( LT(D,0.0) ) return ret;
if( equals(a,0.0) ) {
if( equals(b,0.0) ) return ret;
if( LT(-c/b,0.0) ) return ret;
ret.first = - c / b;
return ret;
}
if( equals(D,0.0) ) D = 0;
ret.first = ( -b - sqrt( D ) ) / ( 2 * a );
ret.second = ( -b + sqrt( D ) ) / ( 2 * a );
if( !equals(ret.first,ret.second) && ret.first > ret.second ) swap(ret.first,ret.second);
return ret;
}
int par[1000];
void init(){ rep(i,N) par[i] = i; }
int find(int x){
if( x==par[x])return par[x];
return par[x] = find(par[x]);
}
void unit(int x,int y){
x = find(x), y = find(y);
if( x != y ) par[x] = y;
}
inline void compute2(){
int cur[N];
rep(i,N) knows[i] = false, cur[i] = 0;
knows[0] = true;
double cx[N], cy[N];
rep(i,N) cx[i] = x[i][0], cy[i] = y[i][0];
init();
rep(i,N) REP(j,i+1,N) {
double dist = getDist(x[i][0]-x[j][0],y[i][0]-y[j][0]);
if( LTE(dist,R) ) unit(i,j);
}
REP(i,1,N) if( find(0) == find(i) ) knows[i] = true;
deque<double> time_stamp;
double prev = 0;
rep(i,N) rep(j,size[i]) if( t[i][j] != 0 ) time_stamp.push_back(t[i][j]);
sort(time_stamp.begin(),time_stamp.end());
time_stamp.erase(unique(time_stamp.begin(),time_stamp.end()),time_stamp.end());
while( 1 ){
double mini = 1e10;
rep(i,N) REP(j,i+1,N) {
if( cur[i]+1 >= size[i] || cur[j]+1 >= size[j] ) continue;
if( knows[i] + knows[j] != 1 ) continue;
dd res = calc(cx[i],cy[i],vx[i][cur[i]+1],vy[i][cur[i]+1],
cx[j],cy[j],vx[j][cur[j]+1],vy[j][cur[j]+1],R);
double lower = max(t[i][cur[i]],t[j][cur[j]]);
double upper = min(t[i][cur[i]+1],t[j][cur[j]+1]);
assert( LTE(lower,prev) && LTE(prev,upper) );
if( LTE(0.0,res.first) ){
if( LTE(lower,prev+res.first) && LTE(prev+res.first,upper) ) mini = min(mini,prev+res.first);
}
if( LTE(0.0,res.second) ){
if( LTE(lower,prev+res.second) && LTE(prev+res.second,upper) ) mini = min(mini,prev+res.second);
}
}
bool initer = false;
if( !time_stamp.empty() && LTE(time_stamp.front(),mini) ) {
mini = time_stamp.front();
time_stamp.pop_front();
initer = true;
}
if( equals(mini,1e10) ) break;
double tm = mini;
rep(j,N){
cx[j] += vx[j][cur[j]+1] * (tm-prev);
cy[j] += vy[j][cur[j]+1] * (tm-prev);
}
init();
rep(j,N) if( knows[j] ) unit(0,j);
rep(j,N)REP(k,j+1,N) {
if( size[j] <= cur[j]+1 || size[k] <= cur[k]+1 ) continue;
if( !( LTE(t[j][cur[j]],tm) && LTE(tm,t[j][cur[j]+1]) ) ) continue;
if( !( LTE(t[k][cur[k]],tm) && LTE(tm,t[k][cur[k]+1]) ) ) continue;
double dist = getDist(cx[j]-cx[k],cy[j]-cy[k]);
if( LTE(dist,R) ) unit(j,k);
}
REP(j,1,N) if( find(0) == find(j) ) knows[j] = true;
rep(i,N) if( cur[i]+1 < size[i] && equals(mini,t[i][cur[i]+1]) ) cur[i]++;
prev = mini;
}
vector<string> vec;
rep(i,N) if( knows[i] ) vec.push_back(nickname[i]);
if(!vec.empty())sort(vec.begin(),vec.end());
rep(i,vec.size()) cout << vec[i] << endl;
}
int main(){
while( scanf("%d%d%d",&N,&T,&R), N|T|R ){
rep(i,N){
cin >> nickname[i];
cin >> t[i][0] >> x[i][0] >> y[i][0];
vx[i][0] = vy[i][0] = 0;
size[i] = 1;
while( cin >> t[i][size[i]] ){
cin >> vx[i][size[i]] >> vy[i][size[i]];
x[i][size[i]] = x[i][size[i]-1] + vx[i][size[i]] * ( t[i][size[i]] - t[i][size[i]-1] );
y[i][size[i]] = y[i][size[i]-1] + vy[i][size[i]] * ( t[i][size[i]] - t[i][size[i]-1] );
++size[i];
if( t[i][size[i]-1] == T ) break;
}
}
compute2();
}
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef double D;
typedef complex<D> P;
D inf=1e50;
D dot(P x,P y){return real(x*conj(y));}
int N,T,R,n[100];
string st[100];
D t[100][1001];
P v[100][1001],p[100][1001];
D fi(D x){
if(abs(x)<1e-9) return 0;
return x;
}
typedef pair<D,D> Pdd;
vector<Pdd> segs[100][100];
void fix(vector<Pdd> vc){
if(vc.empty()) return;
sort(all(vc));
vector<Pdd> ret;
D l=vc[0].fs,r=vc[0].sc;
for(Pdd p:vc){
if(r<p.fs){
ret.pb(Pdd(l,r));
l=p.fs,r=p.sc;
}else{
chmax(r,p.sc);
}
}
ret.pb(Pdd(l,r));
vc=ret;
}
D fst(vector<Pdd> vc,D t){
int N=vc.size();
int id=lower_bound(all(vc),Pdd(t,0))-vc.begin()-1;
if(id>=0&&vc[id].fs<t&&t<vc[id].sc) return t;
id++;
if(id==N) return inf;
return vc[id].fs;
}
int main(){
while(true){
cin>>N>>T>>R;
if(N==0) break;
rep(i,N) rep(j,N) segs[i][j].clear();
rep(i,N){
cin>>st[i];
int j=0;
while(true){
int vx,vy;
cin>>t[i][j]>>vx>>vy;
if(j==0) p[i][0]=P(vx,vy);
else{
v[i][j-1]=P(vx,vy);
p[i][j]=p[i][j-1]+v[i][j-1]*(t[i][j]-t[i][j-1]);
}
if(t[i][j]==T) break;
j++;
}
n[i]=j;
}
rep(a,N) rep(b,a){
rep(i,n[a]) rep(j,n[b]){
D tl=max(t[a][i],t[b][j]);
D tr=min(t[a][i+1],t[b][j+1]);
if(tl>tr) continue;
P s=(p[a][i]-t[a][i]*v[a][i])-(p[b][j]-t[b][j]*v[b][j]),dv=v[a][i]-v[b][j];
D A=fi(norm(dv));
D B=2.0*dot(s,dv);
D C=fi(norm(s)-R*R);
if(A==0){
// if(a==1&&b==0){
// show(i);
// show(j);
// show(C);
// show(p[a][i]);
// show(p[b][j]);
// show(v[a][i]);
// show(v[b][j]);
// }
if(C<=0) segs[a][b].pb(Pdd(tl,tr));
continue;
}
D det=B*B-4*A*C;
if(det<0) continue;
D x=(-B-sqrt(det))/2.0/A,y=(-B+sqrt(det))/2.0/A;
chmax(tl,x);chmin(tr,y);
if(tl<tr) segs[a][b].pb(Pdd(tl,tr));
}
fix(segs[a][b]);
// if(a==1&&b==0){
// for(Pdd p:segs[a][b]) printf("(%.1f,%.1f) ",p.fs,p.sc);
// puts("");
// }
segs[b][a]=segs[a][b];
// cout<<a<<","<<b<<" ";
// show(d[a][b]);
}
bool done[100]={};
D d[100];
rep(i,N) d[i]=inf;
d[0]=0;
rep(i,N){
int mn=-1;
rep(j,N) if(!done[j]&&(mn<0||d[j]<d[mn])) mn=j;
if(mn<0||d[mn]==inf) break;
done[mn]=1;
// show(mn);
rep(j,N) if(!done[j]){
D x=fst(segs[mn][j],d[mn]);
chmin(d[j],x);
}
// if(i==0) rep(j,N) show(d[j]);
}
vector<string> ans;
rep(i,N) if(d[i]<T) ans.pb(st[i]);
sort(all(ans));
for(string s:ans) cout<<s<<endl;
}
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.