【vijos】【图论】【最短路径】【SPFA】想越狱的小杉


   
 
From lolanv
想越狱的小杉 
 
     
   
  背景 Background  
  这次小杉来到了经典美剧《越狱》的场景里…… 
他被抓起来了(-.-干嘛幻想这么郁闷的场景……)。 
小杉身为新一代的Scofield,在挖了半个月之后终于挖通牢房里的地道。 
在地道里,无数的管道路线困惑了他。 

(若对情节有任何疑问,请观看原剧)
     
     
  描述 Description  
  小杉看了看自己的纹身,明白了整个管道网是由N个小房间和若干小房间之间的单向的管道组成的。 
小房间编号为不超过N的正整数。 
每个管道都有一个人品限制值,小杉只能在人品不超过该限制值时通过。 
小杉一开始在房间1,现在小杉想知道,每个小房间他最多能够以人品多少的状态到达。 
注意,小杉的人品在出发以后是不会改变的。
     
     
  输入格式 Input Format  
  每组测试数据的 
第一行有一个正整数N(1<=N<=2000)。 
接下来若干行描述管道,每行三个正整数A,B,R(1<=A,B<=N, 1<=R<1e5, A<>B),表示A房间有一条到达B房间的人品限制值为R的管道(注意从B房间不可由此管道到达A房间,即管道是单向的,每组A,B至多只出现一次)。 
整个输入数据以一行0 0 0结束。 
特别地,对于30%的数据,有N<=100
     

     
  输出格式 Output Format  
  N-1行 
为到达2~n个房间时的最大人品
 

样例输入 Sample Input

4
1 2 30
1 3 20
2 3 25
3 4 30
2 4 20
0 0 0

样例输出 Sample Output

30
25
25

注释 Hint  
  对于样例数据: 
小杉最多能够在人品为30的情况下到达小房间2(1->2) 
小杉最多能够在人品为25的情况下到达小房间3(1->2->3) 
小杉最多能够在人品为25的情况下到达小房间4(1->2->3->4)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;

int N;
struct Edge
{
    int u, v, d; Edge *next; Edge() {}
        Edge(int v, int d, Edge *next):
            v(v), d(d), next(next){}
}*edge[4000];
int dist[4005], S, T;
int queue[1999010];
bool Vst[4005];

void init_file()
{
    freopen("prison.dat", "r", stdin);
    freopen("prison.out", "w", stdout);
}

inline void Ins(int u, int v, int d)
{
    edge[u] = new Edge(v, d, edge[u]);
    //edge[v] = new Edge(u, d, edge[v]);
    return;
}

void read_data()
{
    int x, y, z;
    scanf("%d", &N);
    while(scanf("%d%d%d", &x, &y, &z) == 3)
    {
        if (x == 0 && y == 0 && z == 0) break;
        Ins(x, y, z);
    }
    S = 1;
}

inline void Spfa()
{
    static int f = 0, r = 0, u, v; static Edge *p;
    memset(dist, 0, sizeof dist);
    for (dist[queue[r++] = S] = 999999, Vst[S] = 1; f < r;)
    for (p = edge[u = queue[f++]], Vst[u] = 0; p; p = p -> next)
    {
        int k = min(dist[u], p -> d);
        if (dist[v = p -> v] < k)
        {
            dist[v] = k;
            if (!Vst[v]) Vst[queue[r++] = v] = 1;
        }
    }
    return;
}

void work()
{
    for(int i = 2; i <= N; i++)
    {
        printf("%d\n", dist[i]);
    }
}

int main()
{
    init_file();
    read_data();
    Spfa();
    work();
    return 0;
}

这个题就是改一下SPFA  很不错的一道题

松弛的时候用min(dist[u], p -> d)来松弛dist[v]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值