枚举+dijkstra(最短路删点) hdu5137How Many Maos Does the Guanxi Worth
来源:互联网 发布:excel编程用什么语言 编辑:程序博客网 时间:2024/06/05 15:30
这道题是一道最短路删点的题,题目的大概意思是,刘老板想通过关系网找到校长使自己的孩子能去上学,现在你的任务是删除刘老板到校长的关系网中的任意一人(刘老板和校长除外),使刘老板花费最大,然后求出被你删除之后的刘老板需要花费的最少的钱
解法:因为一共就只有30个点,所以每次删除一个点,然后枚举删除n个点的最短路,再求出这些种情况的最大值即可
#include <iostream>#include <string.h>#include <algorithm>using namespace std;const int inf=1000000000;int e[35][35],e2[35][35];bool book[35];int dis[35],s[35];int n,u,m,t1,t2,t3;int cmp(int a,int b){ return a>b;}void sd(int x)//每次删除一个点{ for(int i=1; i<=n; i++) e[i][x]=e[x][i]=inf;}int dijk(int n,decltype(e) q)//这个地方的处理是让q和e同一种类型,以下是dijk的模板{ //memset(dis,0,sizeof(dis)); for(int i=1; i<=n-1; i++) { int minn=inf; //minn=inf; for(int j=1; j<=n; j++) { if(book[j]==0&&dis[j]<minn) { minn=dis[j]; u=j; } } book[u]=1; for(int v=1; v<=n; v++) { if(q[u][v]<inf) { if(dis[v]>dis[u]+q[u][v]) dis[v]=dis[u]+q[u][v]; } } } return dis[n];}int main(){ while(cin>>n>>m) { int minn=inf,t=0; if(n==0&&m==0) break; for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) { if(i==j) { e[i][j]=e[j][i]=0; e2[i][j]=e2[j][i]=0; } else { e[i][j]=e[j][i]=inf; e2[i][j]=e2[j][i]=inf; } } for(int i=1; i<=m; i++)//要用到两个数组,因为你每次删除一个点之后,要恢复到没删之前,才能对另一个进行操作,否则会影响到结果 { cin>>t1>>t2>>t3; e[t1][t2]=e[t2][t1]=min(t3,e[t1][t2]); e2[t1][t2]=e[t1][t2]; e2[t2][t1]=e[t2][t1]; } for(int k=2; k<n; k++) { for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) e[i][j]=e2[i][j]; sd(k); //memset(dis,0,sizeof(dis)); for(int i=1; i<=n; i++) dis[i]=inf; for(int i=1; i<=n; i++) dis[i]=e[1][i]; for(int i=1; i<=n; i++) book[i]=0; book[1]=1; s[t++]=dijk(n,e); } sort(s,s+n-2,cmp);//注意是n-2个人 //for(int i=1; i<=n; i++) // cout<<s[i]<<" "; //cout<<endl; if(s[0]==inf) cout<<"Inf"<<endl; else cout<<s[0]<<endl; } return 0;}
0 0
- 枚举+dijkstra(最短路删点) hdu5137How Many Maos Does the Guanxi Worth
- HDU5137How Many Maos Does the Guanxi Worth(最短路)
- hdu5137How Many Maos Does the Guanxi Worth【最短路最大值】
- 杭电-5137How Many Maos Does the Guanxi Worth(删点枚举+最短路)
- hdoj 5137 How Many Maos Does the Guanxi Worth 【枚举删点 + 最短路】
- hdu5137How Many Maos Does the Guanxi Worth
- How Many Maos Does the Guanxi Worth 5137 (最短路+删点+最大边)
- HDU 5137 How Many Maos Does the Guanxi Worth(最短路--dijkstra)
- HDOJ 5137 How Many Maos Does the Guanxi Worth(最短路删点)
- hdoj 5137 How Many Maos Does the Guanxi Worth 【最短路 + 删点】
- How Many Maos Does the Guanxi Worth (暴力枚举+Dijkstra)
- hdoj5137How Many Maos Does the Guanxi Worth【最短路】
- 【最短路】HDU5137 How Many Maos Does the Guanxi Worth
- HDU 5137--How Many Maos Does the Guanxi Worth【枚举 && 最短路 && spfa】
- How Many Maos Does the Guanxi Worth (dijkstra)
- hdu 5137 How Many Maos Does the Guanxi Worth (最短路SPAF实现)
- HDOJ 5137 How Many Maos Does the Guanxi Worth(最短路)
- 2014广州站 K - How Many Maos Does the Guanxi Worth(floyed最短路)
- Lexicographic permutations
- 前端神器Sublime Text 3的下载与安装
- C++处理类型
- 远程部署Maven web项目到web服务器
- IFTTT实现新浪微博完美同步Twitter
- 枚举+dijkstra(最短路删点) hdu5137How Many Maos Does the Guanxi Worth
- JeeSite(2):导入数据,进入系统
- 抽象类与接口的区别
- python获取ipv6地址
- 使用 Flume 部署和管理可扩展的 Web 服务
- Qt 5.6 的 Qt.labs.controls 在安卓上效果
- iOS给客户打包ipa 文件
- enq: TX - row lock contention 等待事件
- 成功搭建OSB环境并运行HelloWorld项目(代码,视频)