Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2692 Accepted Submission(s): 971
Mirko overheard in the car that one of the roads is under repairs, and that it is blocked, but didn't konw exactly which road. It is possible to come from Marica's city to Mirko's no matter which road is closed.
Marica will travel only by non-blocked roads, and she will travel by shortest route. Mirko wants to know how long will it take for her to get to his city in the worst case, so that he could make sure that his girlfriend is out of town for long enough.Write a program that helps Mirko in finding out what is the longest time in minutes it could take for Marica to come by shortest route by non-blocked roads to his city.
In the next M lines are three numbers A, B and V, separated by commas. 1 ≤ A,B ≤ N, 1 ≤ V ≤ 1000.Those numbers mean that there is a two-way road between cities A and B, and that it is crossable in V minutes.
#include <stdio.h> #include <math.h> #include <iostream> #include <algorithm> #include <string.h> #include <stdlib.h> #include <queue> using namespace std; const int N = 1005; const int M = 50005; const int INF = 999999999; /*struct Node{ int u,v; }node[M];*/ int graph[N][N]; bool vis[N]; int low[N]; int pre[N]; int n,m; int dijkstra(int s,bool flag){ for(int i=1;i<=n;i++){ if(flag){ pre[i] = s; } low[i] = graph[s][i]; vis[i] = false; } vis[s] = true; for(int i=1;i<n;i++){ int Min = INF; for(int j=1;j<=n;j++){ if(low[j]<Min&&!vis[j]){ Min = low[j]; s = j; } } vis[s] = true; for(int j=1;j<=n;j++){ if(low[j]>low[s]+graph[s][j]&&!vis[j]){ low[j] = low[s]+graph[s][j]; if(flag){ pre[j] = s; } } } } return low[n]; } int main() { while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i==j) graph[i][j] = 0; else graph[i][j] = INF; } } for(int i=0;i<m;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); graph[u][v]=graph[v][u] = min(w,graph[u][v]); //scanf("%d%d%d",&node[i].u,&node[i].v,&w); //graph[node[i].u][node[i].v]=graph[node[i].v][node[i].u] = min(w,graph[node[i].u][node[i].v]); } int res = dijkstra(1,1); int Max = -1; /*for(int i=0;i<m;i++){ ///枚举每一段TLE int temp = graph[node[i].u][node[i].v]; graph[node[i].u][node[i].v]=graph[node[i].v][node[i].u] = INF; int cost = dijkstra(1); Max = max(cost,Max); graph[node[i].u][node[i].v]=graph[node[i].v][node[i].u] = temp; }*/ int temp = n; while(temp!=1){ int k = graph[temp][pre[temp]]; graph[pre[temp]][temp] = graph[temp][pre[temp]] = INF; int cost = dijkstra(1,0); Max = max(cost,Max); graph[pre[temp]][temp] = graph[temp][pre[temp]] =k; temp = pre[temp]; } printf("%d\n",Max); } }
----update----
利用链式前向星可以考虑到重边效果,虽然这里并没有.
#include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> #include <stdio.h> #include <queue> using namespace std; const int maxn = 1005; const int INF = 0xfffffff; int n,m; struct Edge{ int u,v,w; int next; }edge[maxn*maxn]; int id[maxn*maxn]; //如果当前最短路中包含点v,那么 id[v] 则是 v 所用到的边的编号. int head[maxn],tot = 0; void addedge(int u,int v,int w,int &k){ edge[k].v = v,edge[k].w = w; edge[k].next = head[u],head[u] = k++; } void init(){ memset(head,-1,sizeof(head)); memset(id,-1,sizeof(id)); tot = 0; } bool vis[maxn]; int d[maxn],pre[maxn]; int spfa(int s,int t,bool flag){ memset(vis,false,sizeof(vis)); for(int i=0;i<=maxn;i++){ d[i] = INF; if(flag){ pre[i] = s; } } queue<int> q; q.push(s); vis[s] = true; d[s] = 0; while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = false; for(int k=head[u];k!=-1;k=edge[k].next){ int v = edge[k].v,w=edge[k].w; if(d[u]+w<d[v]){ d[v] = d[u]+w; if(!vis[v]){ vis[v] = true; q.push(v); } if(flag){ pre[v] = u; id[v] = k; } } } } if(d[t]>=INF) return -1; return d[t]; } int main(){ while(scanf("%d %d",&n,&m)!=EOF){ init(); for(int i=0;i<m;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w,tot); addedge(v,u,w,tot); } int res; res = spfa(1,n,1); int MAX = res; int temp=n; while(temp!=1){ int w = edge[id[temp]].w; edge[id[temp]].w = INF; MAX = max(spfa(1,n,0),MAX); edge[id[temp]].w = w; temp = pre[temp]; } printf("%d\n",MAX); } return 0; }