社区讨论

为什么改一条单向边也可以AC

P1186玛丽卡参与者 4已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@mi6lo21f
此快照首次捕获于
2025/11/20 06:54
4 个月前
此快照最后确认于
2025/11/20 06:54
4 个月前
查看原帖
##跑完spfa之后记录的路径中只改了那一条单向边,应该是改双向边
为什么也对
CPP
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 2000010
#define maxn1 2050
using namespace std;
int head[maxn],vis[maxn],dis[maxn],team[maxn];
int fa[maxn1],f[maxn1];
int newn[maxn];
struct node
{
    int next,cost,to;
}e[maxn];
int cnt=1;
int n,m;
int add(int u,int v,int w)
{
    e[++cnt].next=head[u];
    e[cnt].to=v;
    e[cnt].cost=w;
    head[u]=cnt;
}
int spfa(int s)
{
    memset(vis,0,sizeof(vis));
    memset(dis,0x7f,sizeof(dis));
    int h=0,t=1;
    team[1]=s;
    vis[s]=1;
    dis[s]=0;
    while(h<t)
    {
        h++;
        int u=team[h];
        vis[u]=0;
        for(int i=head[u];i;i=e[i].next)
        {
            int to=e[i].to;
            if(dis[to]>dis[u]+e[i].cost)
            {
                fa[to]=u;
                f[to]=i;
                dis[to]=dis[u]+e[i].cost;
                if(vis[to]==0)
                {
                    t++;
                    vis[to]=1;
                    team[t]=to;    
                }
            }
        }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    spfa(n);
    int now=1;
    cnt=0;
    while(now!=n)
    {
        newn[++cnt]=f[now];
        now=fa[now];
    }
    int maxx=0;
    for(int i=1;i<=cnt;i++)
    {
//        cout<<"#"<<endl;
        int k=e[newn[i]].cost;
        e[newn[i]].cost=0x7ffffff;
//        e[newn[i]^1].cost=0x7fffffff;/
        spfa(n);
        e[newn[i]].cost=k;
//        e[newn[i]^1].cost=k;
        maxx=max(maxx,dis[1]);
    }
    cout<<maxx<<endl;
    return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...