[2176] Luo Gu roadblock

Title Description

Every morning, FJ from their homes across the farm went to the bullpen. Agricultural farms consisting of N blocks, M are connected by bi-directional farm road, each road has a certain length. FJ house No. 1 field, the field number N in the cow. The two fields are not connected to a plurality of roads to the appropriate path for any pair of sequence always walk on the farm field. When another piece when FJ come from a field, always with a total path length of the shortest road to go order.

FJ cattle do, always ill-wishers, she decided to interfere with his plans each morning. They are placed in a stack of straw on a road M, so that doubling the length of the road. Cattle hope you follow a path interference makes FJ road from home to the largest increase in the bullpen long. They ask you to design and tell them how much the maximum increment.

Input Format

Line 1: two integers N, M.

2 through M + 1: Line i + 1 line contains three integers A_i, B_i, L_i, A_i and B_i i represents the number of fields of road connection, L_i represents path length.

Output Format

Line 1: An integer representing the maximum increment by doubling a route obtained.

Sample input and output

Input # 1
5 7
2 1 5
1 3 1
3 2 8
3 5 7
3 4 3
2 4 7
4 5 2
Output # 1
2

Description / Tips

[Sample Description]

Ruoshi road between 3 and 4 long doubled by the shortest path becomes 1-3-4-5 1-3-5.

[Data] scale and conventions

For 30% of the data, N <= 70, M <= 1,500.

To 100% of the data, 1 <= N <= 100,1 <= M <= 5,000,1 <= L_i <= 1,000,000.

 

 

Solution: The last point TLE, a withdrawing organ can live, and happy.

          Ordinary SPFA plus a small simulation on it oh

 

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N=10001;
int x,n,m,u,v,zz,cnt,ans;
int dis[N],head[N],vis[N];
struct node{
    int val;
    int next;
    int to;
}e[N];

void add(int x,int y,int z){
    e[++cnt].to=y;
    e[cnt].val=z;
    e[cnt].next=head[x];
    head[x]=cnt;
}
queue<int>q;
int spfa(){
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    vis[1]=1; dis[1]=0; q.push(1);
    while(!q.empty()){
        x=q.front();
        q.pop(); vis[x]=0;
        for(int i=head[x];i!=0;i=e[i].next){
            v=e[i].to;
            if(e[i].val+dis[x]<dis[v]){
                dis[v]=e[i].val+dis[x];
                if(vis[v]==0) { vis[v]=1; q.push(v); }
            } 
        } 
    }
    return dis[n];
}

int main(){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d %d %d",&u,&v,&zz);
        add(u,v,zz); add(v,u,zz);
    }
    int qwq=spfa();
    for(int i=1;i<=m;i++){
        e[i*2].val*=2;
        e[i*2-1].val*=2;
        int jjj=spfa();
        ans=max(jjj,ans);
        //cout<<ans<<endl;
        e[i*2].val/=2;
        e[i*2-1].val/=2;
    }
    printf("%d\n",ans-qwq);
    return 0;
}

Ha (secretly attach an optimized version, but wrong 90 points ...... no one should see it)

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N=10001;
int x,n,m,u,v,zz,cnt,ans;
int dis[N],head[N],vis[N];

struct node{
    int val;
    int next;
    int to;
}e[N];

void add(int x,int y,int z){
    e[++cnt].to=y;
    e[cnt].val=z;
    e[cnt].next=head[x];
    head[x]=cnt;
}

queue<int>q;
int pre[N],fr[N],po,ioi[N],nu;

int spfa(){
    memset(dis,0x3f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    vis[1]=1; dis[1]=0; q.push(1);
    while(!q.empty()){
        x=q.front();
        q.pop(); vis[x]=0;
        for(int i=head[x];i;i=e[i].next){
            v=e[i].to;
            if(e[i].val+dis[x]<dis[v]){
                dis[v]=e[i].val+dis[x];
                pre[v]=i; fr[v]=x;//***
                if(!vis[v]) { vis[v]=1; q.push(v); }
            } 
        } 
    }
    return dis[n];
}

int main(){
    freopen("2176.in","r",stdin);
    freopen("2176.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d %d %d",&u,&v,&zz);
        add(u,v,zz); add(v,u,zz);
    }
    
    int qwq=spfa();
    int now=n;
    while(now!= ++ not] = price [now];
        ioi [) {1//记路径
        now=fr[now];
    }
    
    for(int i=1;i<=nu;i++){
        e[ioi[i]].val*=2;
        e[ioi[i]^1].val*=2;
        int jjj=spfa();
        ans=max(jjj,ans);
        //cout<<ans<<endl;
        e[ioi[i]].val/=2;
        e[ioi[i]^1].val/=2;
    }
    printf("%d\n",ans-qwq);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11680339.html