//比较好的一个模板题,原来的Dijkstra + priority一直用邻接前向星实现
//这道题尝试用矩阵实现,也成功了
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int maxv=509;
const int INF=0x3f3f3f3f;
int mmap[maxv][maxv],dis[maxv];
bool visit[maxv];
void init() {
memset(mmap,INF,sizeof mmap);
memset(dis,INF,sizeof dis);
}
struct Node{
int u,dis;
bool operator< (const Node rhs) const{
return this->dis>rhs.dis;
}
}now,temp;
void Dijkstra(int s,int n,int mmap[][maxv],int dist[maxv],bool visit[]) {
int i,v;
dist[s]=0;
priority_queue<Node> pq;
temp.dis=0,temp.u=s;
pq.push(temp);
while(!pq.empty()) {
temp=pq.top();
pq.pop();
if(visit[temp.u]==true)
continue;
visit[temp.u]=true;
for(v=1;v<=n;v++){
if(visit[v]==false&&dist[v]>dist[temp.u]+mmap[temp.u][v]){
dist[v]=dist[temp.u]+mmap[temp.u][v];
now.u=v;
now.dis=dist[v];
pq.push(now);
}
}
}
}
int main(void) {
#ifndef ONLINE_JUDGE
freopen("E:\\input.txt","r",stdin);
#endif // ONLINE_JUDGE
ios::sync_with_stdio(false);
int n,temp_int;
char temp[10];
cin>>n;
init();
for(int i=2; i<=n; i++)
for(int j=1; j<i; j++) {
cin>>temp;
if(temp[0]=='x')
mmap[i][j]=mmap[j][i]=INF;
else {
sscanf(temp,"%d",&temp_int);
mmap[i][j]=mmap[j][i]=temp_int;
}
}
Dijkstra(1,n,mmap,dis,visit);
int ans=*(max_element(dis+1,dis+1+n));
cout<<ans<<endl;
return 0;
}
POJ 1502 Dijkstra 矩阵存图 priority
猜你喜欢
转载自blog.csdn.net/shadandeajian/article/details/81288385
今日推荐
周排行