QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#325438#5613. Road To Savingsape_packWA 1ms3972kbC++143.1kb2024-02-11 12:42:002024-02-11 12:42:01

Judging History

你现在查看的是最新测评结果

  • [2024-02-11 12:42:01]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3972kb
  • [2024-02-11 12:42:00]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = a; i >= b; i--)
#define F0Rd(i,a) for (int i = a; i >= 0; i--)
#define FORit(it,a) for (auto it = a.begin(); it != a.end(); it++)
#define trav(a,x) for (auto& a: x)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define alla(arr, sz) arr, arr + sz

const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!!
const ll linf = 4*1e18;
const int inf = 1000000007;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

#define DEBUG 0

int start, finish; 
vector<vector<pii>> parent; 
vector<bool> visited; 
int sum_pave;

vector<vi> fwd; 
vector<bool> fwd_visited; 

void dfs(int v) {
    if (v == start) return;
    visited[v] = 1; 
    trav(x,parent[v]) if (fwd_visited[x.ff] && !visited[x.ff]) {
        sum_pave += x.ss;
        dfs(x.ff);
    }
}

void dfs2(int v) {
    fwd_visited[v] = 1; 
    trav(x,fwd[v]) if (!fwd_visited[x]) {
        dfs2(x);
    }
}

void solve() {
    int n, m; cin >> n >> m >> start >> finish; start--; finish--; 
    vector<pii> adj[n];
    int sum_all_edges = 0; 

    F0R(i,m) {
        int a, b, c; cin>> a >> b >> c;
        a--; b--;
        sum_all_edges += c; 
        adj[a].pb(mp(b,c));
        adj[b].pb(mp(a,c));
    }
 
    parent.resize(n); 
    fwd.resize(n);

    ll dist[n];
    dist[0] = 0; 
    FOR(i,1,n) dist[i] = linf;
 
    set<pll> s; // (cost, to)
    s.insert(mp(0,0));
    while (s.size() != 0) {
        pll p = *s.begin(); 
        s.erase(s.begin());
        ll v = p.ss, c = p.ff; 
 
        trav(x, adj[v]) {
            int to = x.ff, cost = x.ss; 
            ll new_price = c + cost; 
            if (new_price < dist[to]) { 
                s.erase(mp(dist[to], to)); 
                dist[to] = new_price; 
                s.insert(mp(dist[to], to));

                parent[to].clear(); 
                parent[to].pb(mp(v,cost));
            }
            else if (new_price == dist[to]) {
                parent[to].pb(mp(v,cost)); 
            }
        }
    }

    F0R(i,n) {
        trav(x,parent[i]) {
            fwd[x.ff].pb(i);
        }
    }

    fwd_visited.assign(n,0);
    dfs2(start);

    visited.assign(n,0);
    sum_pave = 0;
    dfs(finish);
    cout << sum_all_edges - sum_pave; 
}

int main() {
  #if DEBUG
  setIn("test.in");
  #else
  setIO();
  #endif

  int t;
  t= 1;  
  //cin >> t;
  while (t--) 
    solve();

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3876kb

input:

4 5 1 4
1 2 1
1 3 2
1 4 2
4 2 1
3 4 1

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

4 5 1 4
1 2 1
1 3 2
1 4 1
4 2 1
3 4 1

output:

5

result:

ok single line: '5'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3568kb

input:

2 1 1 2
1 2 10

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3632kb

input:

3 3 1 2
1 2 10
2 3 10
3 1 10

output:

20

result:

ok single line: '20'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3836kb

input:

3 3 1 2
1 2 10
2 3 5
3 1 5

output:

0

result:

ok single line: '0'

Test #6:

score: -100
Wrong Answer
time: 1ms
memory: 3972kb

input:

100 4950 74 24
1 2 8
1 3 6
1 4 5
1 5 6
1 6 2
1 7 6
1 8 1
1 9 10
1 10 1
1 11 10
1 12 9
1 13 4
1 14 4
1 15 1
1 16 4
1 17 7
1 18 8
1 19 1
1 20 7
1 21 2
1 22 9
1 23 9
1 24 7
1 25 6
1 26 5
1 27 4
1 28 3
1 29 4
1 30 6
1 31 8
1 32 10
1 33 10
1 34 3
1 35 6
1 36 10
1 37 5
1 38 4
1 39 1
1 40 10
1 41 9
1 42 7
...

output:

27619

result:

wrong answer 1st lines differ - expected: '27615', found: '27619'