hdu1595(spfa) find the longest of the shortest
2014-11-06 22:17
323 查看
题目链接:
huangjing
思路:因为这个女人是从最短路走,那么首先找出最短路,然后拆边,然后再找最短路,最后求出最长时间即可。
代码里还有递归法求最短路径。。
题目:
Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1934 Accepted Submission(s): 680
Problem Description
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she doesn't live in the same city, she started preparing for the long journey.We know for every road how many minutes it takes to come from one city to another.
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.
Input
Each case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N, Mirko is located in city 1,
and Marica in city N.
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.
Output
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
Sample Input
Sample Output
Author
ailyanlu
Source
HDU 2007-Spring Programming Contest - Warm
Up (1)
Recommend
8600 | We have carefully selected several similar problems for you: 1599 1596 1598 1142 1217
代码:
huangjing
思路:因为这个女人是从最短路走,那么首先找出最短路,然后拆边,然后再找最短路,最后求出最长时间即可。
代码里还有递归法求最短路径。。
题目:
find the longest of the shortest
Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1934 Accepted Submission(s): 680
Problem Description
Marica is very angry with Mirko because he found a new girlfriend and she seeks revenge.Since she doesn't live in the same city, she started preparing for the long journey.We know for every road how many minutes it takes to come from one city to another.
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.
Input
Each case there are two numbers in the first row, N and M, separated by a single space, the number of towns,and the number of roads between the towns. 1 ≤ N ≤ 1000, 1 ≤ M ≤ N*(N-1)/2. The cities are markedwith numbers from 1 to N, Mirko is located in city 1,
and Marica in city N.
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.
Output
In the first line of the output file write the maximum time in minutes, it could take Marica to come to Mirko.
Sample Input
5 6 1 2 4 1 3 3 2 3 1 2 4 4 2 5 7 4 5 1 6 7 1 2 1 2 3 4 3 4 4 4 6 4 1 5 5 2 5 2 5 6 5 5 7 1 2 8 1 4 10 2 3 9 2 4 10 2 5 1 3 4 7 3 5 10
Sample Output
11 13 27
Author
ailyanlu
Source
HDU 2007-Spring Programming Contest - Warm
Up (1)
Recommend
8600 | We have carefully selected several similar problems for you: 1599 1596 1598 1142 1217
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<vector> #include<cmath> #include<string> #include<queue> #define eps 1e-9 #define ll long long #define INF 0x3f3f3f3f using namespace std; const int maxn=1000+10; bool vis[maxn],used[maxn][maxn]; int dis[maxn],n,m,pre[maxn],cnt,head[maxn]; queue<int>Q; struct Edge { int to,next,val; }edge[maxn*maxn]; void add_edge(int x,int y,int z) { edge[++cnt].to=y; edge[cnt].val=z; edge[cnt].next=head[x]; head[x]=cnt; } int spfa(int ok) { while(!Q.empty()) Q.pop(); memset(vis,false,sizeof(vis)); memset(dis,INF,sizeof(dis)); vis[1]=true; dis[1]=0; Q.push(1); if(ok) pre[1]=-1; while(!Q.empty()) { int top=Q.front(); Q.pop(); for(int i=head[top];i!=-1;i=edge[i].next) { int xx=edge[i].to; int yy=edge[i].val; if(used[top][xx]&&dis[xx]>dis[top]+yy) { dis[xx]=dis[top]+yy; if(ok) pre[xx]=top; if(!vis[xx]) { Q.push(xx); vis[xx]=true; } } } vis[top]=false; } return dis ; } void print(int x) { if(pre[x]==-1) { printf("1"); return; } print(pre[x]); printf("-->%d",x); } int main() { int ans,u,v,w; while(~scanf("%d%d",&n,&m)) { memset(head,-1,sizeof(head)); cnt=0; for(int i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); add_edge(u,v,w); add_edge(v,u,w); } for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) used[i][j]=used[j][i]=true; ans=spfa(1); // print(n); for(int i=n;pre[i]!=-1;i=pre[i]) { u=i,v=pre[i]; used[u][v]=used[v][u]=false; ans=max(ans,spfa(0)); used[u][v]=used[v][u]=true; } printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU1595:find the longest of the shortest(spfa+记录路径)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest (dijkstra + spfa)
- hdu1595 find the longest of the shortest(Dijkstra)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- HDU1595 - find the longest of the shortest - 最短路变形+暴力
- hdu1595 find the longest of the shortest
- find the longest of the shortest --spfa删边
- HDU1595 find the longest of the shortest
- HDU1595 find the longest of the shortest
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu 1595 find the longest of the shortest (spfa)
- HDU1595:find the longest of the shortest(Dijkstra)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu1595-find the longest of the shortest
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)
- hdu1595 find the longest of the shortest(Dijkstra)
- hdu 1595 find the longest of the shortest(spfa)(存储并枚举最短路径的边)