最短路spfa POJ 1502 MPI Maelstrom
2016-03-12 15:00
435 查看
题意:一个信息站需要往其他信息站传输信息。当一个站接受到信息之后马上向跟它相连的信息站传输信息。已知没两个相连信息站之间传输时间。求:信息从第一个站传到每个站的最短时间。
思路:将传输代价作为路径长度,原来是求第一个信息站到其它所有信息站最短路径的最大值。
解法:用spfa,784K 0MS
思路:将传输代价作为路径长度,原来是求第一个信息站到其它所有信息站最短路径的最大值。
解法:用spfa,784K 0MS
#include<iostream> #include<cstdio> #include<vector> #include<cstring > #include<queue> using namespace std; const int MAX=205; const int INF=2100000000; bool vis[MAX];//判断是不是在队列中 int dis[MAX],n,tmp,ans=-1; struct edge{ int to; int w; edge(int a=0,int b=0):to(a),w(b) {} };//存每个边的权值和指向的点 vector<edge>E[MAX]; void add(int from,int to,int w){ E[from].push_back(edge(to,w)); } void spfa(){ queue<int> q; q.push(1); vis[1]=true; int from,to; while(!q.empty()){ from=q.front();//队首出队列 q.pop(); vis[from]=false; for(int i=0;i<E[from].size();i++){ to=E[from][i].to; if(dis[to]>dis[from]+E[from][i].w){//如果可以松弛 dis[to]=dis[from]+E[from][i].w;//进行松弛 if(!vis[to]){//如果不在队列里面 vis[to]=true;//入队列 q.push(to); } } } } } int main(){ freopen("i.txt","r",stdin); cin>>n; for(int i=2;i<=n;i++)dis[i]=INF,vis[i]=false; char str[50]; int power[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000},len; for(int i=2;i<=n;i++){ for(int j=1;j<i;j++){ scanf("%s",str); tmp=0,len=0; if(str[0]!='x') { len=strlen(str); for(int k=len-1;k>=0;k--)tmp+=power[len-1-k]*(str[k]-'0');//转换成数字 add(i,j,tmp);//由于是无向图,需要双向加边 add(j,i,tmp); } } } spfa(); for(int i=2;i<=n;i++) ans=ans>dis[i]?ans:dis[i];//寻找最大值 cout<<ans<<endl; return 0; }
相关文章推荐
- 安装OpenMPI来配合C语言程序进行并行计算
- MPICH2(r8733)的错误容忍
- MPI单机多机方法
- MPI并行实现枚举排序
- CSU1307 并查集+SPFA
- MPI函数列表
- 最短路径 -- spfa
- 单源最短路深度分析
- MVAPICH2多线程支持问题
- 基于单边Jacobi旋转的并行SVD算法-MPI框架
- USACO/butter 3.2.6
- MPI集体通讯(一)
- MS-MPI+boost 编译
- Application of Boost MPI
- Boost mpi gatherv example
- Boost skeleton-content 问题
- 【POJ1860】Currency Exchange
- hdu 1596 find the safest road(spfa算法)
- MPI并行程序的调试技巧
- MZ Training 2014 #8 F题