답안 #25600

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
25600 2017-06-23T07:24:33 Z 시제연(#1075) 버스 (JOI14_bus) C++
35 / 100
1000 ms 62912 KB
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> pii;
typedef pair<pii,int> piii;

struct edge {
    int a, c, o;
    edge(){}
    edge(int a, int c, int o):a(a),c(c),o(o){}
    inline bool operator < (const edge &A) const {return c!=A.c?c<A.c:o<A.o;}
};

int n, m, c, q;
vector<pii> lis[600100];
vector<pii> tis[100100];
pii ord[600100];
int dis[600100];
int deg[600100];
int INF = 987654321;
edge edg[600100];
int son[600100];

queue<int> pq;
void dijk() {
    int i;
    for (i=0;i<2*m;i++) {
        dis[i] = INF;
        if (ord[i].first==0) dis[i] = 0;
        if (deg[i]==0) pq.push(i);
    }
    while(!pq.empty()) {
        int here = pq.front();
        pq.pop();
        for (i=0;i<lis[here].size();i++) {
            int there = lis[here][i].second;
            int w = lis[here][i].first;
            if (dis[there]>dis[here]+w) dis[there]=dis[here]+w;
            deg[there]--;
            if (deg[there]==0) pq.push(there);
        }
    }
}

int qu[300100];

bool cmp(int a, int b) {
    return edg[a]<edg[b];
}
int main() {
    int i, j;

    //freopen("input","r",stdin);

    scanf("%d%d",&n,&m);
    for (i=0;i<m;i++) {
        int a, b, c, d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        a--;b--;
        edg[i*2] = edge(a,c,1);
        edg[i*2+1] = edge(b,d,0);
        lis[i*2].push_back(pii(d-c,i*2+1));
        ord[i*2] = pii(a,c);
        ord[i*2+1] = pii(b,d);
        deg[i*2+1]++;
        son[i*2] = i*2;
        son[i*2+1] = i*2+1;
    }
    sort(son,son+2*m,cmp);
    for (i=0;i<2*m;i++) {
        tis[edg[son[i]].a].push_back(pii(edg[son[i]].c,son[i]));
    }
    for (i=1;i<n;i++) {
        for (j=(int)tis[i].size()-1;j>0;j--) {
            lis[tis[i][j-1].second].push_back(pii(tis[i][j].first-tis[i][j-1].first,tis[i][j].second));
            deg[tis[i][j].second]++;
        }
    }
    dijk();
    /*
    for (i=0;i<c;i++) {
        for (j=0;j<lis[i].size();j++) {
            printf("%d (%d,%d) -> %d (%d,%d) : %d\n",i,ord[i].first+1,ord[i].second,
                                                     lis[i][j].second,ord[lis[i][j].second].first+1,
                                                     ord[lis[i][j].second].second,lis[i][j].first);
        }
    }
    for (i=0;i<c;i++) {
        printf("%d, %d : %d\n",ord[i].first+1,ord[i].second,dis[i]);
    }
    */
    for (j=0;j<tis[n-1].size();j++) qu[j] = tis[n-1][j].first;
    c = tis[n-1].size();
    scanf("%d",&q);
    for (i=0;i<q;i++) {
        int a;
        scanf("%d",&a);
        int idx = upper_bound(qu,qu+c,a)-qu-1;
        if (idx<0) {
            printf("-1\n");
            continue;
        }
        if (dis[tis[n-1][idx].second]>a) {
            printf("-1\n");
            continue;
        }
        printf("%d\n",tis[n-1][idx].first-dis[tis[n-1][idx].second]);
    }

    return 0;
}

Compilation message

bus.cpp: In function 'void dijk()':
bus.cpp:36:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (i=0;i<lis[here].size();i++) {
                   ^
bus.cpp: In function 'int main()':
bus.cpp:93:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (j=0;j<tis[n-1].size();j++) qu[j] = tis[n-1][j].first;
               ^
bus.cpp:56:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&m);
                        ^
bus.cpp:59:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d%d",&a,&b,&c,&d);
                                      ^
bus.cpp:95:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&q);
                   ^
bus.cpp:98:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&a);
                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 38360 KB Output is correct
2 Correct 6 ms 38360 KB Output is correct
3 Correct 6 ms 38360 KB Output is correct
4 Correct 6 ms 38360 KB Output is correct
5 Correct 13 ms 38360 KB Output is correct
6 Correct 3 ms 38360 KB Output is correct
7 Correct 3 ms 38360 KB Output is correct
8 Correct 9 ms 38360 KB Output is correct
9 Correct 3 ms 38360 KB Output is correct
10 Correct 3 ms 38360 KB Output is correct
11 Correct 6 ms 38492 KB Output is correct
12 Correct 6 ms 38492 KB Output is correct
13 Correct 13 ms 38624 KB Output is correct
14 Correct 9 ms 38492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 38360 KB Output is correct
2 Correct 46 ms 38360 KB Output is correct
3 Correct 36 ms 38360 KB Output is correct
4 Correct 9 ms 38360 KB Output is correct
5 Correct 6 ms 38360 KB Output is correct
6 Correct 13 ms 38360 KB Output is correct
7 Correct 36 ms 38360 KB Output is correct
8 Correct 3 ms 38360 KB Output is correct
9 Correct 39 ms 38360 KB Output is correct
10 Correct 39 ms 38492 KB Output is correct
11 Correct 29 ms 38624 KB Output is correct
12 Correct 49 ms 38492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1000 ms 62912 KB Execution timed out
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1000 ms 62912 KB Execution timed out
2 Halted 0 ms 0 KB -