复制样例数据
5 7
2 1 5
1 3 1
3 2 8
3 5 7
3 4 3
2 4 7
4 5 2
2
There are 5 fields and 7 pathways. Currently, the shortest path from the house (field 1) to the barn (field 5) is 1-3-4-5 of total length 1+3+2=6.If the cows double the length of the pathway from field 3 to field 4 (increasing its length from 3 to 6), then FJ‘s shortest route is now 1-3-5, of total length 1+7=8, larger by two than the previous shortest route length.
思路:改变的一定是最短路上面的(否则仍然走最短路就好了)。其次我们记录最短路,枚举改变的边,取最大值,再减去原来的最短路径长度。
#include<iostream> #include<cstring> #include<cstring> #include<cstdio> #include<cmath> #include<vector> #include<queue> #include<map> #include<algorithm> #define REP(i, a, b) for(int i = (a); i <= (b); ++ i) #define REP(j, a, b) for(int j = (a); j <= (b); ++ j) #define PER(i, a, b) for(int i = (a); i >= (b); -- i) using namespace std; const int maxn = 5100; template <class T> inline void rd(T &ret){ char c; ret = 0; while ((c = getchar()) < ‘0‘ || c > ‘9‘); while (c >= ‘0‘ && c <= ‘9‘){ ret = ret * 10 + (c - ‘0‘), c = getchar(); } } struct edge{ int v,w,next; }p[maxn*2]; int n,m,cnt=1,d[maxn],ans,head[maxn],vis[maxn],cur,fa[maxn],e[maxn],w[maxn]; void add(int u,int v,int w){ p[++cnt].v=v,p[cnt].w=w,p[cnt].next=head[u],head[u]=cnt; } queue<int>que; void spfa(int s){ memset(vis,0,sizeof(vis)); memset(d,127,sizeof(d)); que.push(s); vis[s]=1; d[s]=0; while(que.size()){ int cur=que.front(); que.pop(); vis[cur]=0; for(int i=head[cur];i;i=p[i].next){ int to=p[i].v; if(d[to]>d[cur]+p[i].w){ d[to]=d[cur]+p[i].w; fa[to]=cur; e[to]=i; if(!vis[to]){ que.push(to); vis[to]=1; } } } } } int main() { rd(n),rd(m); REP(i, 1, m){ int u,v,w; cin>>u>>v>>w; add(u,v,w),add(v,u,w); } spfa(1); ans=d[n]; int r=0; for(int i=n;i;i=fa[i])w[++r]=e[i]; REP(i, 1, r){ p[w[i]].w*=2,p[w[i]^1].w*=2; spfa(1); cur=max(cur,d[n]); p[w[i]].w/=2,p[w[i]^1].w/=2; } cout<<cur-ans<<endl; }