Submission #616357
Source Code Expand
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
template<typename Value, typename Compare = std::less<Value>, typename Index = int>
class BinaryHeap {
Compare cmp;
vector<Value> a;
vector<Index> idx, nodeidx;
int pos;
public:
BinaryHeap(Compare cmp_ = Compare()) : cmp(cmp_) {}
BinaryHeap(int n, Compare cmp_ = Compare()) : cmp(cmp_) { init(n); }
void init(int n) {
a.resize(n + 1);
idx.assign(n + 1, -1);
nodeidx.assign(n + 1, -1);
pos = 1;
}
const Value &min() const { return a[1]; }
const Value &get(Index i) const { return a[nodeidx[i]]; }
Index argmin() const { return idx[1]; }
Index size() const { return pos - 1; }
bool empty() const { return pos == 1; }
const Value &add(Index i, const Value &x) {
Index node = nodeidx[i];
if(node >= 0) return a[node];
a[pos] = x;
idx[pos] = i;
nodeidx[i] = pos;
++ pos;
up(pos - 1);
return x;
}
const Value &update(Index i, const Value &x) {
Index node = nodeidx[i];
if(node < 0) {
a[pos] = x;
idx[pos] = i;
nodeidx[i] = pos;
++ pos;
up(pos - 1);
} else {
a[node] = x;
up(node);
down(node);
}
return x;
}
const Value &updatemin(Index i, const Value &x) {
Index node = nodeidx[i];
if(node >= 0 && !cmp(x, a[node])) return a[node];
else return update(i, x);
}
Value remove(Index i) {
if(i < 0) return Value();
Index node = nodeidx[i];
if(node < 0) return Value();
-- pos;
idx[node] = idx[pos];
nodeidx[idx[pos]] = node;
nodeidx[i] = -1;
idx[pos] = -1;
swap(a[pos], a[node]);
up(node);
down(node);
return a[pos];
}
private:
void up(Index i) {
for(Index c = i, p = c >> 1; p > 0 && cmp(a[c], a[p]); c >>= 1, p >>= 1) {
swap(a[p], a[c]);
swap(nodeidx[idx[p]], nodeidx[idx[c]]);
swap(idx[p], idx[c]);
}
}
void down(Index i) {
Index bottom = pos;
for(Index c = i; c * 2 < bottom; ) {
Index b = c * 2 + (c * 2 + 1 < bottom && cmp(a[c * 2 + 1], a[c * 2]));
if(!cmp(a[b], a[c])) break;
swap(a[c], a[b]);
swap(nodeidx[idx[c]], nodeidx[idx[b]]);
swap(idx[c], idx[b]);
c = b;
}
}
};
template<typename Weight_>
struct WeightedTo {
typedef int Vertex; typedef Weight_ Weight;
Vertex to; Weight w;
WeightedTo() {}
WeightedTo(Vertex to_, Weight w_) : to(to_), w(w_) {}
Weight getWeight() const { return w; }
};
template<typename To_>
struct StaticGraph {
typedef To_ To;
typedef typename To::Vertex Vertex;
typedef std::pair<Vertex, To> Edge;
typedef const To *const_iterator;
void init(int n_, const std::vector<Edge> &edges) {
n = n_; int m = edges.size();
tos.resize(m + 1); offsets.assign(n + 1, 0);
for(int e = 0; e < m; e ++) offsets[edges[e].first] ++;
for(int v = 1; v <= n_; v ++) offsets[v] += offsets[v - 1];
for(int e = 0; e < m; e ++)
tos[-- offsets[edges[e].first]] = edges[e].second;
}
int numVertices() const { return n; }
int numEdges() const { return tos.size() - 1; }
inline const_iterator edgesBegin(int v) const { return &tos[offsets[v]]; }
inline const_iterator edgesEnd(int v) const { return &tos[offsets[v + 1]]; }
private:
int n;
std::vector<To> tos;
std::vector<int> offsets;
};
typedef StaticGraph<WeightedTo<int> > Graph;
template<typename Dist, typename Graph>
void dijkstra(const Graph &g, int s, vector<Dist> &dist) {
int n = g.numVertices();
dist.assign(n, numeric_limits<Dist>::max());
vector<bool> vis(n);
BinaryHeap<Dist> h(n);
h.add(s, 0);
while(!h.empty()) {
int v = h.argmin(); Dist d = h.min();
dist[v] = d;
h.remove(v);
vis[v] = true;
for(typename Graph::const_iterator it = g.edgesBegin(v), et = g.edgesEnd(v); it != et; ++ it) {
if(!vis[it->to])
h.updatemin(it->to, d + it->getWeight());
}
}
}
int main() {
int N; int M; int src; int dst;
while(~scanf("%d%d%d%d", &N, &M, &src, &dst)) {
vector<Graph::Edge> edges;
vector<vector<pair<pii,pii> > > g(N);
rep(i, M) {
int L;
scanf("%d", &L);
vector<int> s(L);
for(int i = 0; i < L; ++ i)
scanf("%d", &s[i]);
vector<int> w(L - 1);
for(int i = 0; i < L - 1; ++ i)
scanf("%d", &w[i]);
int sum;
sum = 0;
for(int i = L - 2; i >= 0; -- i) {
sum += w[i];
g[s[i]].push_back(mp(mp(s[i + 1], w[i]), mp(s[L - 1], sum)));
}
sum = 0;
for(int i = 1; i < L; ++ i) {
sum += w[i - 1];
g[s[i]].push_back(mp(mp(s[i - 1], w[i - 1]), mp(s[0], sum)));
}
rep(i, L - 1) {
edges.push_back(Graph::Edge(s[i], Graph::To(s[i + 1], w[i])));
edges.push_back(Graph::Edge(s[i + 1], Graph::To(s[i], w[i])));
}
}
Graph gg;
gg.init(N, edges);
vector<int> distDst;
dijkstra(gg, dst, distDst);
int low = 0, up = 2525 * N * 3;
while(up - low > 0) {
int mid = (low + up) / 2;
priority_queue<pair<int, int> > q;
vector<bool> vis(N);
vector<int> dist(N, INF);
dist[src] = 0;
q.push(mp(-0, src));
while(!q.empty()) {
int d = -q.top().first;
int i = q.top().second;
q.pop();
if(vis[i]) continue;
vis[i] = true;
each(j, g[i]) {
int nd = d + j->first.second;
int sd = d + j->second.second + distDst[j->second.first];
if(nd <= mid && sd <= mid && dist[j->first.first] > nd) {
dist[j->first.first] = nd;
q.push(mp(-nd, j->first.first));
}
}
}
if(vis[dst])
up = mid;
else
low = mid + 1;
}
printf("%d\n", up);
}
return 0;
}
Submission Info
Submission Time |
|
Task |
C - メンテナンス明け |
User |
anta |
Language |
C++11 (GCC 4.9.2) |
Score |
100 |
Code Size |
6691 Byte |
Status |
AC |
Exec Time |
263 ms |
Memory |
6108 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:193:19: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &L);
^
./Main.cpp:196:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &s[i]);
^
./Main.cpp:199:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &w[i]);
^
Judge Result
Set Name |
Subtask1 |
Subtask2 |
Score / Max Score |
50 / 50 |
50 / 50 |
Status |
|
|
Set Name |
Test Cases |
Subtask1 |
small/00_sample00, small/00_sample01, small/00_sample02, small/10_small-0000, small/10_small-0001, small/10_small-0002, small/10_small-0003, small/10_small-0004, small/10_small-0005, small/10_small-0006, small/10_small-0007, small/10_small-0008, small/10_small-0009, small/10_small-0010, small/10_small-0011, small/10_small-0012, small/10_small-0013, small/10_small-0014, small/10_small-0015, small/10_small-0016, small/10_small-0017, small/10_small-0018, small/10_small-0019, small/30_max_small, small/40_simple_0000, small/40_simple_0001, small/40_simple_0002, small/40_simple_0003, small/40_simple_0004, small/40_simple_0005, small/40_simple_0006, small/40_simple_0007, small/40_simple_0008, small/40_simple_0009, small/40_simple_0010, small/40_simple_0011, small/40_simple_0012, small/40_simple_0013, small/40_simple_0014, small/40_simple_0015, small/40_simple_0016, small/40_simple_0017, small/40_simple_0018, small/40_simple_0019, small/90_dijkstra_killer_00, small/90_dijkstra_killer_01, small/91_tayama_killer_00, small/91_tayama_killer_01, small/91_tayama_killer_02, small/91_tayama_killer_03, small/91_tayama_killer_04, small/91_tayama_killer_05 |
Subtask2 |
large/20_large-00, large/20_large-01, large/20_large-02, large/20_large-03, large/20_large-04, large/31_max_large |
Case Name |
Status |
Exec Time |
Memory |
large/20_large-00 |
AC |
217 ms |
6108 KB |
large/20_large-01 |
AC |
229 ms |
6032 KB |
large/20_large-02 |
AC |
263 ms |
5976 KB |
large/20_large-03 |
AC |
248 ms |
5972 KB |
large/20_large-04 |
AC |
251 ms |
5984 KB |
large/31_max_large |
AC |
59 ms |
4008 KB |
small/00_sample00 |
AC |
25 ms |
796 KB |
small/00_sample01 |
AC |
25 ms |
792 KB |
small/00_sample02 |
AC |
25 ms |
796 KB |
small/10_small-0000 |
AC |
26 ms |
736 KB |
small/10_small-0001 |
AC |
27 ms |
800 KB |
small/10_small-0002 |
AC |
26 ms |
916 KB |
small/10_small-0003 |
AC |
26 ms |
916 KB |
small/10_small-0004 |
AC |
27 ms |
800 KB |
small/10_small-0005 |
AC |
26 ms |
924 KB |
small/10_small-0006 |
AC |
27 ms |
804 KB |
small/10_small-0007 |
AC |
26 ms |
804 KB |
small/10_small-0008 |
AC |
26 ms |
800 KB |
small/10_small-0009 |
AC |
26 ms |
804 KB |
small/10_small-0010 |
AC |
24 ms |
916 KB |
small/10_small-0011 |
AC |
26 ms |
920 KB |
small/10_small-0012 |
AC |
26 ms |
800 KB |
small/10_small-0013 |
AC |
27 ms |
808 KB |
small/10_small-0014 |
AC |
27 ms |
804 KB |
small/10_small-0015 |
AC |
25 ms |
796 KB |
small/10_small-0016 |
AC |
25 ms |
924 KB |
small/10_small-0017 |
AC |
26 ms |
808 KB |
small/10_small-0018 |
AC |
26 ms |
928 KB |
small/10_small-0019 |
AC |
26 ms |
800 KB |
small/30_max_small |
AC |
25 ms |
928 KB |
small/40_simple_0000 |
AC |
25 ms |
912 KB |
small/40_simple_0001 |
AC |
24 ms |
800 KB |
small/40_simple_0002 |
AC |
25 ms |
796 KB |
small/40_simple_0003 |
AC |
26 ms |
784 KB |
small/40_simple_0004 |
AC |
27 ms |
924 KB |
small/40_simple_0005 |
AC |
24 ms |
924 KB |
small/40_simple_0006 |
AC |
26 ms |
720 KB |
small/40_simple_0007 |
AC |
26 ms |
800 KB |
small/40_simple_0008 |
AC |
26 ms |
916 KB |
small/40_simple_0009 |
AC |
27 ms |
920 KB |
small/40_simple_0010 |
AC |
25 ms |
800 KB |
small/40_simple_0011 |
AC |
27 ms |
924 KB |
small/40_simple_0012 |
AC |
25 ms |
924 KB |
small/40_simple_0013 |
AC |
29 ms |
928 KB |
small/40_simple_0014 |
AC |
27 ms |
812 KB |
small/40_simple_0015 |
AC |
24 ms |
800 KB |
small/40_simple_0016 |
AC |
25 ms |
808 KB |
small/40_simple_0017 |
AC |
25 ms |
928 KB |
small/40_simple_0018 |
AC |
27 ms |
808 KB |
small/40_simple_0019 |
AC |
27 ms |
916 KB |
small/90_dijkstra_killer_00 |
AC |
28 ms |
916 KB |
small/90_dijkstra_killer_01 |
AC |
27 ms |
920 KB |
small/91_tayama_killer_00 |
AC |
27 ms |
916 KB |
small/91_tayama_killer_01 |
AC |
27 ms |
920 KB |
small/91_tayama_killer_02 |
AC |
27 ms |
916 KB |
small/91_tayama_killer_03 |
AC |
27 ms |
920 KB |
small/91_tayama_killer_04 |
AC |
26 ms |
812 KB |
small/91_tayama_killer_05 |
AC |
26 ms |
804 KB |