【luogu P1186 玛丽卡】 题解

题目链接:https://www.luogu.org/problemnew/show/P1186
邻接表开大开小真的奇妙,毒瘤玩意,再您妈的见。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1010;
const int inf = 2139062143;
struct edge{
    int len, from, to, next;
}e[1000001];
int head[maxn], dis[maxn], n, m, cnt, u, v, w, ans, s, cut[maxn][maxn], f[maxn];//f[]记录路径
bool vis[maxn], flag;
queue<int> q;
void add(int u, int v, int w)
{
    e[++cnt].from = u;
    e[cnt].len = w;
    e[cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}
int SPFA()
{
    queue<int> q;
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f3f3f3f,sizeof(dis));
    vis[1] = 1;
    q.push(1);
    dis[1] = 0; 	
    while(!q.empty())
    {
        int now = q.front();
        q.pop();
        vis[now] = 0;
        for(int i = head[now]; i ; i = e[i].next)
        {
            if(cut[now][e[i].to] == 0&&dis[e[i].to] > dis[now]+e[i].len)
            {
            	if(!flag) f[e[i].to] = now;
                dis[e[i].to] = dis[now]+e[i].len;
                if(!vis[e[i].to])
                {
                    vis[e[i].to] = 1;
                    q.push(e[i].to);
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    memset(dis,0x7f,sizeof(dis));
    for(int i = 1; i <= m; i++)
    {
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w);
        add(v,u,w);
    }
    SPFA();
    flag = 1;
    for(int i = n; i != 1; i = f[i])
    {
        cut[f[i]][i] = 1;
        cut[i][f[i]] = 1;
        SPFA();
        cut[f[i]][i] = 0;
        cut[i][f[i]] = 0;
        ans = max(dis[n], ans);
    }
    printf("%d\n",ans);
    return 0;
}
posted @ 2018-06-22 11:32  Misaka_Azusa  阅读(185)  评论(0编辑  收藏  举报
Live2D