freopen

Revision en2, by MVP_Harry, 2020-12-10 07:32:26

Hi,

About three weeks ago, I asked a related question here. In that post, my program output to stdout, while it needs to be output into a file. A lot of people gives me suggestions to use freopen("Filename.in", "r", stdin); freopen("Filename.out", "w", stdout); instead of ifstream ...

That's what I did and things have been ok for the past three weeks. However, when I tried to submit a solution using freopen, this happens:

freopen

I go check my solution and find no error that would cause "runtime error or memory limited exceeded". Therefore, I submitted it again, only replacing freopen with ifstream. Then, it gets accepted:

ifstream.

This might be a noobish question, but what causes this and how can I avoid that? Thank you!

P.S. This is the code:

#include<bits/stdc++.h>
#define ll long long
#define rep(i, m, n) for (int i = m; i <= n; i++)
#define per(i, m, n) for (int i = m; i >= n; i--)
#define FOR(i, m, n, k) for (int i = m; i <= n; i += k)
#define ROF(i, m, n, k) for (int i = m; i >= n; i -= k)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define INF 0x3f3f3f3f
using namespace std;

const int maxn = 3e4 + 10;

struct edge {
    int to, cost;
};

int N, T;
int a[105][105], dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int dis[maxn];
vector<edge> G[maxn];

int get(int x, int y, int t) {
    return (x - 1) * N + y + t * N * N;
}

void dijkstra(int s) {
    priority_queue<pii, vector<pii>, greater<pii> > q;
    memset(dis, INF, sizeof(dis));
    dis[s] = 0;
    q.push(make_pair(0, s));
    while (!q.empty()) {
        pii p = q.top();
        q.pop();
        int u = p.second;
        if (dis[u] < p.first) continue;
        for (auto node : G[u]) {
            int v = node.to;
            if (dis[v] > dis[u] + node.cost) {
                dis[v] = dis[u] + node.cost;
                q.push(make_pair(dis[v], v));
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    // ifstream cin("visitfj.in");
    // ofstream cout("visitfj.out");
    freopen("visistfj.in", "r", stdin);
    freopen("visistfj.out", "w", stdout);
    cin >> N >> T;
    rep(i, 1, N) rep(j, 1, N) cin >> a[i][j];
    rep(i, 1, N) {
        rep(j, 1, N) {
            rep(k, 0, 3) {
                int x = i + dx[k], y = j + dy[k];
                if (x < 1 || x > N || y < 1 || y > N) continue;
                rep(t, 0, 2) {
                    int nxt = (t + 1) % 3;
                    int cost = T + ((nxt == 0) ? a[x][y] : 0);
                    G[get(i, j, t)].pb((edge) {get(x, y, nxt), cost});
                }
            }
        }
    }
    dijkstra(get(1, 1, 0));
    int ans = INF;
    rep(i, 0, 2) ans = min(dis[get(N, N, i)], ans);
    cout << ans << "\n";
    return 0;
}
Tags usaco

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English MVP_Harry 2020-12-10 07:32:26 4 Tiny change: '```freopen", this hap' -> '```freopen```, this hap'
en1 English MVP_Harry 2020-12-10 07:31:46 3086 Initial revision (published)