codevs 1021 玛丽卡

                                                                                                       图论水题:玛丽卡

。。。。。。。。好吧,不太水 ;


题目描述 Description

麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复。

    因为她和他们不住在同一个城市,因此她开始准备她的长途旅行。

    在这个国家中每两个城市之间最多只有一条路相通,并且我们知道从一个城市到另一个城市路上所需花费的时间。

    麦克在车中无意中听到有一条路正在维修,并且那儿正堵车,但没听清楚到底是哪一条路。无论哪一条路正在维修,从玛丽卡所在的城市都能到达麦克所在的城市。

    玛丽卡将只从不堵车的路上通过,并且她将按最短路线行车。麦克希望知道在最糟糕的情况下玛丽卡到达他所在的城市需要多长时间,这样他就能保证他的女朋友离开该城市足够远。

编写程序,帮助麦克找出玛丽卡按最短路线通过不堵车道路到达他所在城市所需的最长时间(用分钟表示)。

输入描述 Input Description

第一行有两个用空格隔开的数N和M,分别表示城市的数量以及城市间道路的数量。1≤N≤1000,1≤M≤N*(N-1)/2。城市用数字1至N标识,麦克在城市1中,玛丽卡在城市N中。

接下来的M行中每行包含三个用空格隔开的数A,B和V。其中1≤A,B≤N,1≤V≤1000。这些数字表示在A和城市B中间有一条双行道,并且在V分钟内是就能通过。

 

输出描述 Output Description

   输出文件的第一行中写出用分钟表示的最长时间,在这段时间中,无论哪条路在堵车,玛丽卡应该能够到达麦克处,如果少于这个时间的话,则必定存在一条路,该条路一旦堵车,玛丽卡就不能够赶到麦克处。

样例输入 Sample Input

5 7

1 2 8

1 4 10

2 3 9

2 4 10

2 5 1

3 4 7

3 5 10

样例输出 Sample Output

27



思路: 先来一遍SPFA,找最短路, 然后再枚举这条最短路上的边, 依次删除一条边, 再找最短路, 统计一下最大答案即可;

思路很重要, 不会怎么办, 看题解, 看到会为之-_-   -_-  !


vector版代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

const int maxn=1010;
const int INF=0x3f3f3f3f;

int n, m;
struct nb {
    int from, to, dist;
};
vector<nb> G[maxn];
int d[maxn], inq[maxn], fa[maxn];
int ans, f[maxn][maxn];

void SPFA(int u)
{
    for(int i=1; i<=n; i++) d[i]=INF;
    d[u]=0;
    inq[u]=1;
    queue<int> q;
    q.push(u);
    while(!q.empty()) {
        int x=q.front(); q.pop(); inq[x]=0;
        for(int i=0; i< (int) G[x].size(); i++) {
            int y=G[x][i].to;
            if(d[y]>d[x]+f[x][y]) {
                d[y]=d[x]+f[x][y];
                fa[y]=x;
                if(!inq[y]) q.push(y), inq[y]=1;
            }
        }
    }
}

void dfs(int x, int y)
{
    int temp=f[x][y];
    f[x][y]=f[y][x]=INF;
    SPFA(1);
    f[x][y]=f[y][x]=temp;
    if(d[n]!=INF) ans=max(ans, d[n]);
}

int main()
{
    scanf("%d%d", &n, &m);
    for(int i=1; i<=m; i++) {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        f[x][y]=f[y][x]=z;
        G[x].push_back( (nb){x, y, z});
        G[y].push_back( (nb){y, x, z});
    }
    SPFA(1);
    int x=fa[n], y=n;
    while(y!=1) {
        dfs(x, y);
        y=x; x=fa[x];
    }
    cout<<ans;
    return 0;
}


前向星: 高逼格版


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int maxn=1010;
const int INF=0x3f3f3f3f;
const int maxm=1000010;

int n, m;
struct nb {
    int from, to, dist, next, id;
};
nb G[maxm];
int cnt, head[maxn];
int d[maxn], inq[maxn], fa[maxn];
int ans;

void add(int from, int to, int dist, int v)
{
    G[cnt].from=from;
    G[cnt].dist=dist;
    G[cnt].to=to;
    G[cnt].next=head[from];
    G[cnt].id=cnt+v;
    head[from]=cnt++;
}

void SPFA(int u, int v)
{
    for(int i=1; i<=n; i++) d[i]=INF;
    d[u]=0;
    inq[u]=1;
    queue<int> q;
    q.push(u);
    while(!q.empty()) {
        int x=q.front(); q.pop(); inq[x]=0;
        for(int i=head[x]; i!=-1; i=G[i].next) {
            int y=G[i].to;
            if(d[y]>d[x]+G[i].dist) {
                d[y]=d[x]+G[i].dist;
                if(!v) fa[y]=i;
                if(!inq[y]) q.push(y), inq[y]=1;
            }
        }
    }
}

void dfs(int x)
{
    int temp=G[x].dist;
    G[x].dist=G[G[x].id].dist=INF;
    SPFA(1, 1);
    G[x].dist=G[G[x].id].dist=temp;
    if(d[n]!=INF) ans=max(ans, d[n]);
}

int Qin()
{
    int x=0;
    char c=getchar();
    while(c<'0' || c>'9') c=getchar();
    while(c>='0' && c<='9') x=x*10+c-'0', c=getchar();
    return x;
}

void init()
{
    memset(head, -1, sizeof(head));
    memset(fa, -1, sizeof(fa));
    n=Qin(); m=Qin();
    for(int i=1; i<=m; i++) {
        int x, y, z;
        x=Qin(); y=Qin(); z=Qin();
        add(x, y, z, 1);
        add(y, x, z, -1);
    }
}

int main()
{
    //freopen("mlk.in", "r", stdin);
    //freopen("mlk.out", "w", stdout);
    init();
    SPFA(1, 0);
    fa[1]=-1;
    int x=fa[n];
    while(x!=-1) {
        dfs(x); 
        x=fa[G[x].from];
    }
    cout<<ans;
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值