poj 1502
来源:互联网 发布:体育专业表格数据分析 编辑:程序博客网 时间:2024/05/29 18:14
这是一道最短路的题目,而且带点字符串的处理,从中我学到了一个新的函数,aoti()具体如下:http://baike.baidu.com/view/653935.htm代码如下:#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;const int maxn=105;const int inf=0x3f3f3f3f;int map[maxn][maxn];int dist[maxn];bool p[maxn];int n;char s[maxn];void dj(int v){ int i,j,min,pos; for(i=0; i<n; i++) { p[i]=false; dist[i]=map[v][i]; } p[v]=true; dist[v]=0; for(i=0; i<n-1; i++) { min=inf; for(j=0; j<n; j++) { if(!p[j]&&dist[j]<min) { min=dist[j]; pos=j; } } p[pos]=true; for(j=0; j<n; j++) { if(!p[j]&&dist[j]>dist[pos]+map[pos][j]) { dist[j]=dist[pos]+map[pos][j]; } } }}int main(){ int i,j; scanf("%d",&n); for(i=1; i<n; i++) { for(j=0; j<i; j++) { scanf("%s",s); if(s[0]=='x') { map[i][j]=map[j][i]=inf; } else { map[i][j]=map[j][i]=atoi(s); } } } dj(0); int res=0; for(i=0; i<n; i++) { res=max(res,dist[i]); } cout<<res<<endl; return 0;}