zoukankan      html  css  js  c++  java
  • codevs 1021 玛丽卡

    一看2s的时限,就神暴力就可以吧。。先跑spfa求出最短路,然后每次抹去一条边,再跑最短路,求出最大值即可。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #define maxe 500050
    #define maxv 1005
    using namespace std;
    struct edge
    {
    int v,w,nxt;
    }e[maxe];
    int g[maxv],n,m,x,y,z,nume=0;
    int pree[maxv*10],dis[maxv],maxn=0,prev[maxv*10];
    void addedge(int u,int v,int w)
    {
    e[++nume].v=v;
    e[nume].nxt=g[u];
    e[nume].w=w;
    g[u]=nume;
    }
    void spfa(int type,int limit)
    {
    memset(dis,0x3f,sizeof(dis));
    dis[1]=0;
    if (type==0)
    {
    queue <int> q;
    q.push(1);
    while (!q.empty())
    {
    int head=q.front();
    q.pop();
    for (int i=g[head];i;i=e[i].nxt)
    {
    int v=e[i].v;
    if (dis[v]>dis[head]+e[i].w)
    {
    dis[v]=dis[head]+e[i].w;
    pree[v]=i;
    prev[v]=head;
    q.push(v);
    }
    }
    }
    }
    else
    {
    queue <int> q;
    q.push(1);
    while (!q.empty())
    {
    int head=q.front();
    q.pop();
    for (int i=g[head];i;i=e[i].nxt)
    {
    int v=e[i].v;
    if ((dis[v]>dis[head]+e[i].w) && (i!=limit))
    {
    dis[v]=dis[head]+e[i].w;
    q.push(v);
    }
    }
    }
    if (dis[n]!=1061109567)
    maxn=max(maxn,dis[n]);
    }
    }
    int main()
    {
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++)
    {
    scanf("%d%d%d",&x,&y,&z);
    addedge(x,y,z);
    addedge(y,x,z);
    }
    spfa(0,0);
    for (int i=n;i;i=prev[i])
    spfa(1,pree[i]);
    printf("%d",maxn);
    return 0;
    }

  • 相关阅读:
    Python字符串
    ListCtrl控件
    leetcode1004
    leetcode1003
    leetcode1002
    leetcode153
    leetcode540
    leetcode435
    leetcode999
    leetcode997
  • 原文地址:https://www.cnblogs.com/ziliuziliu/p/5093096.html
Copyright © 2011-2022 走看看