[SPFA的SLF优化] Codeforces Round #257 (Div. 1) B

B. Jzzhu and Cities

题目链接 : http://codeforces.com/contest/449/problem/B

题目大意 : n个城市由公路连接,现有一些从首都到各个城市的铁路,询问哪些铁路可以被删掉,铁路可以被删掉当且仅当最短路径不改变时

思路 :建图做一遍最短路,考虑在最短路图上:

           1°  如果有一个点存在公路到达,那么到这个点的公路都可以被删掉

           2°  如果某点在最短路图上全是铁路连接,那么这些铁路中至少保留一条

           另外这题果的SPFA过不了,要加上SLF优化(见下面)

Code :

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <deque>
using namespace std;
#define foru(i, a, b) for (int i=(a); i<=(b); i++)
#define ford(i, a, b) for (int i=(a); i>=(b); i--)
#define clr(a) memset(a, 0, sizeof(a));
#define N 100010
#define M 1000010
typedef long long ll;
const ll INF = 100000000ll * 100000000ll;

int n, m, t, tot;
int adj[N], next[M], aim[M], num[N];
ll len[M];
bool rou[M];

void add(int a, int b, ll c){
    tot ++;
    len[tot] = c;
    aim[tot] = b;
    next[tot] = adj[a];
    adj[a] = tot;
}

void init(){
    clr(num); clr(rou);
    scanf("%d%d%d", &n, &m, &t);
    foru(i, 1, m){
        int u, v; ll c;
        scanf("%d%d%I64d", &u, &v, &c);
        add(u, v, c);
        add(v, u, c);
    }
    foru(i, 1, t){
        int x; ll c;
        scanf("%d%I64d", &x, &c);
        num[x] ++;
        add(1, x, c); rou[tot] = 1;
        add(x, 1, c); rou[tot] = 1;
    }
}

deque<int> s;
ll dis[N];
bool v[N];
bool sta[N];

void spfa(){
    clr(v); v[1] = 1; s.push_back(1);
    foru(i, 2, n) dis[i] = INF; dis[1] = 0ll;
    while (! s.empty()){
        int x = s.front();
        s.pop_front(); v[x] = 0;
        int k = adj[x];
        while (k){
            int tx = aim[k];
            if (dis[x] + len[k] == dis[tx]){
                if (! rou[k]) sta[tx] = 1;
            }
            if (dis[x] + len[k] < dis[tx]){
                    dis[tx] = dis[x] + len[k];
                    if (! v[tx]){
                        v[tx] = 1;
                        if (! s.empty()) {
                            if (dis[tx] > dis[s.front()]) s.push_back(tx);
                                else s.push_front(tx);
                        } else s.push_back(tx);
                    }
                    if (! rou[k]) sta[tx] = 1;
                        else sta[tx] = 0;
            }
            k = next[k];
        }
    }
}

void work(){
    int ans = 0;
    foru(i, 1, n)
        if (sta[i]) ans += num[i];
            else if (num[i] > 0) ans += (num[i]-1);
    printf("%d\n", ans);
}

int main(){
  //  freopen("B.txt", "r", stdin);
    init();
    spfa();
    work();
    return 0;
}


Tips:

SPFA的SLF优化:在入队操作时,对于入队的点j比较对首点i,若Dj <= Di 将j点插入对首,否者将j点插入队尾

void spfa(){
    clr(v); v[1] = 1; s.push_back(1);
    foru(i, 2, n) dis[i] = INF; dis[1] = 0ll;
    while (! s.empty()){
        int x = s.front();
        s.pop_front(); v[x] = 0;
        int k = adj[x];
        while (k){
            int tx = aim[k];
            if (dis[x] + len[k] < dis[tx]){
                    dis[tx] = dis[x] + len[k];
                    if (! v[tx]){
                        v[tx] = 1;
                        if (! s.empty()) {
                            if (dis[tx] > dis[s.front()]) s.push_back(tx);
                                else s.push_front(tx);
                        } else s.push_back(tx);
                    }
            }
            k = next[k];
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值