• 【洛谷 1186】玛丽卡


    题目描述

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

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

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

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

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

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

    输入格式

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

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

    输出格式

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

    输入输出样例

    输入 #1
    5 7
    1 2 8
    1 4 10
    2 3 9
    2 4 10
    2 5 1
    3 4 7
    3 5 10
    
    输出 #1
    27


    题解:我一开始真的每条边都删去,结果80分,后来想一想,只要删去
    有用的边就可以了,因此有了这个小优化qwq
    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int oo=0x3f3f3f3f;
    int n,m,k,cnt,x,y,z,s,t,dis[2300],vis[2300],f[500*999];
    struct node{
        int to;
        int val;
        int next;
    }e[1000003];
    int head[2300],cut[1001][1001],flag;
    void add(int a,int b,int c){
        cnt++;
        e[cnt].to=b;
        e[cnt].val=c;
        e[cnt].next=head[a];
        head[a]=cnt;
    }
    queue<int>q;
    int SPFA(){
        memset(vis,0,sizeof(vis));
        memset(dis,0x3f,sizeof(dis));
        q.push(1);
        dis[1]=0;
        vis[1]=1;
        while(!q.empty()){
            x=q.front();
            q.pop();
            vis[x]=0;
            for(int i=head[x];i!=0;i=e[i].next){
                int too=e[i].to;
                if(cut[x][too] == 0 && dis[too]>dis[x]+e[i].val){ 
                    if(flag==1) f[too]=x;
                    dis[too]=dis[x]+e[i].val;
                    if(vis[too]==0){
                        vis[too]=1; q.push(too);
                    }
                } 
            }
        }
        return dis[n];
    }
    inline int read(){
        int Out = 0,f = 1;char ch = getchar();
        while(!isdigit(ch)){if(ch == '-')f = -1;ch = getchar();}
        while(isdigit(ch)){Out = Out * 10 + ch - '0';ch = getchar();}
        return Out * f;
    }
    int main(){
        freopen("1186.in","r",stdin);
        freopen("1186.out","w",stdout);
        n=read(); m=read(); //scanf("%d %d",&n,&m);
        for(int i=1;i<=m;i++){
        x=read(); y=read(); z=read();  //    scanf("%d %d %d",&x,&y,&z);
            add(x,y,z); add(y,x,z);
        }
        int ans=0;
        /*for(int i=1;i<=m*2;i+=2){
            int a1=e[i].val;
            int a2=e[i+1].val;
            e[i].val=oo;
            e[i+1].val=oo;
            
            e[i].val=a1;
            e[i+1].val=a2;
        }*/
        flag=1;
        ans=SPFA();
        flag=0;
        for(int i=n;i!=1;i=f[i]){
            cut[f[i]][i]=1;
            cut[i][f[i]]=1;
            ans=max(ans,SPFA());
            cut[f[i]][i]=0;
            cut[i][f[i]]=0;
        } 
        printf("%d
    ",ans);
        return 0;
    }


  • 相关阅读:
    十年微软(Microsoft)MVP
    HTML5使用Div标签来实现表格
    C# 6.0的字典(Dictionary)的语法
    C# 6.0的属性(Property)的语法与初始值
    ASP.NET MVC使用jQuery实现Autocomplete
    The system cannot find the file specified
    Web实时通信
    实时数据显示--SignalR实例演示
    No Javascript on this page
    The SQL Server Service Broker for the current database is not enabled, and as a result query notifications are not supported.
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11700561.html
Copyright © 2020-2023  润新知