hdu 2433 Travel(最短路径数思想)
来源:互联网 发布:vscode 网页 编辑:程序博客网 时间:2024/06/05 18:51
Travel
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2616 Accepted Submission(s): 861
Problem Description
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) towns in it. Each town products one kind of food, the food will be transported to all the towns. In addition, the trucks will always take the shortest way. There are M (M <= 3000) two-way roads connecting the towns, and the length of the road is 1.
Let SUM be the total distance of the shortest paths between all pairs of the towns. Please write a program to calculate the new SUM after one of the M roads is destroyed.
Let SUM be the total distance of the shortest paths between all pairs of the towns. Please write a program to calculate the new SUM after one of the M roads is destroyed.
Input
The input contains several test cases.
The first line contains two positive integers N, M. The following M lines each contains two integers u, v, meaning there is a two-way road between town u and v. The roads are numbered from 1 to M according to the order of the input.
The input will be terminated by EOF.
The first line contains two positive integers N, M. The following M lines each contains two integers u, v, meaning there is a two-way road between town u and v. The roads are numbered from 1 to M according to the order of the input.
The input will be terminated by EOF.
Output
Output M lines, the i-th line is the new SUM after the i-th road is destroyed. If the towns are not connected after the i-th road is destroyed, please output “INF” in the i-th line.
Sample Input
5 45 11 33 25 42 21 21 2
Sample Output
INFINFINFINF22
思路:常规的做法是每次销毁一条边就求n次最短路,但是这样时间复杂度达到了O(mn²logn),必然会TLE。
所以我们可以预处理求n次最短路并记录下每一次经过了哪些边,那么如果我们销毁的边不在最短路径上就不必再求一次最短路了。如果销毁的路有多条,也不必再求了。
所以我们只需对仅有一条且在最短路径上的边进行最短路的求解。 这也被称为最短路径树的思想。注意这里我用迪杰斯特拉就会超时,用spfa就过了,可能是因为对于这道题,边比较少。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;#define N 110#define M 3050#define INF 1<<28int n,m;int d[N],vis[N];int cnt,head[N];int used[N][N][N];int sum[N],pre[N],num[N][N];struct Edge{ int v,next,w;} edge[M*2];void init(){ cnt=0; memset(head,-1,sizeof(head)); memset(used,0,sizeof(used)); memset(num,0,sizeof(num)); memset(sum,0,sizeof(sum));}void addedge(int u,int v,int w){ edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++;}int spfa(int s,int t){ queue<int> q; for(int i=1;i<=n;i++) d[i]=INF,vis[i]=0; d[s]=0,vis[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v,w=edge[i].w; if(d[u]+w<d[v]) { d[v]=d[u]+w; if(!vis[v]) { if(!t) used[s][u][v]=used[s][v][u]=1; q.push(v); vis[v]=1; } } } } int ans=0; for(int i=1; i<=n; i++) { if(d[i]==INF) return INF; ans+=d[i]; } return ans;}int main(){ int u,v,mark,ans,s,t; while(~scanf("%d %d",&n,&m)) { ans=0; init(); for(int i=0; i<m; i++) { scanf("%d %d",&u,&v); addedge(u,v,1); addedge(v,u,1); num[u][v]++,num[v][u]++; } for(int i=1; i<=n; i++) { sum[i]=spfa(i,0); if(sum[i]==INF) { ans=INF; break; } ans+=sum[i]; } for(int i=0; i<m; i++) { s=ans; if(ans==INF) { printf("INF\n"); continue; }; if(num[edge[i*2+1].v][edge[i*2].v]>1) { printf("%d\n",ans); continue; } edge[i*2].w=edge[i*2+1].w=INF; for(int j=1; j<=n; j++) if(used[j][edge[i*2+1].v][edge[i*2].v]) { t=spfa(j,1); if(t==INF)break; s=s-sum[j]+t; } if(t==INF) printf("INF\n"); else printf("%d\n",s); edge[i*2].w=edge[i*2+1].w=1; } } return 0;}
0 0
- hdu 2433 Travel(最短路径数思想)
- hdu 2433 Travel(最短路径树+删边+bfs)
- hdu 2433 Travel (最短路径树)
- 【HDU 2433】Travel (BFS+最短路径树)
- HDU 5441 Travel(最短路径变形)
- hdu2433 Travel(最短路径dijkstra,好题)
- 最短路径(最基础,经典的模板和思想):HDU-2544最短路
- HDU2433 Travel 最短路径树
- hdu 最短路径
- hdu 最短路径
- 最短路径条数(floyd)
- HDU - 2433 Travel (最短路树)
- HDU-2433 Travel(最短路[Dijkstra])
- HDU2112 HDU Today(最短路径)
- hdu 2066(最短路径)
- HDU Today (最短路径问题)
- HDU Today (最短路径+map)
- 最短路径条数
- 织梦dedecms数据库类$dsql使用方法步骤
- DX名词解释:渲染目标(Render Target)
- Device Tree(三):代码分析
- iOS 扫码登录
- Android和html5的js交互
- hdu 2433 Travel(最短路径数思想)
- Hibernate基础知识(1)
- SQL多表连接查询(详细实例)
- JavaScript学习--Item20 作用域与作用域链(scope chain)
- Git查看、删除、重命名远程分支和tag
- 无线通信
- Android Studio如何设置代码自动提示
- DedeCMS数据库类常用函数说明
- iOS中点击移动,使得手机屏幕的颜色随机变化