标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7471 | Accepted: 4550 |
Description
Input
Output
Sample Input
5 50 30 5 100 20 50 10 x x 10
Sample Output
35
题意:给出结点数,用邻接矩阵给出每个节点的距离,因为结点i到结点i的距离为0以及路径是双向的所以只给出邻接矩阵对角线的左下半块。
下面分别用求最短路径的方法实现
/* dijkstra 1502 Accepted 428K 0MS G++ */ #include"cstdio" #include"cstring" #include"algorithm" using namespace std; const int MAXN=105; const int INF=0x3fffffff; int mp[MAXN][MAXN]; int V; int dijkstra(int s) { int d[MAXN]; int vis[MAXN]; for(int i=1;i<=V;i++) { vis[i]=0; d[i]=mp[s][i]; } int n=V; while(n--) { int mincost,k; mincost=INF; for(int i=1;i<=V;i++) { if(!vis[i]&&mincost>d[i]) { mincost=d[i]; k=i; } } vis[k]=1; for(int i=1;i<=V;i++) { if(!vis[i]&&d[i]>d[k]+mp[k][i]) { d[i]=d[k]+mp[k][i]; } } } int ans=-1; for(int i=1;i<=V;i++) ans=max(ans,d[i]); return ans; } int main() { while(scanf("%d",&V)!=EOF) { for(int i=1;i<=V;i++) for(int j=1;j<=i;j++) if(i==j) mp[i][j]=0; else{ char x[10]; scanf("%s",x); if(x[0]==‘x‘) mp[i][j]=mp[j][i]=INF; else mp[i][j]=mp[j][i]=atoi(x); } printf("%d\n",dijkstra(1)); } return 0; }
/* 堆优化dijkstra 1502 1502 Accepted 632K 0MS G++ */ #include"cstdio" #include"cstring" #include"algorithm" #include"vector" #include"queue" using namespace std; const int MAXN=105; const int INF=0X3fffffff; struct Edge{ int to,cost; Edge(){} Edge(int to,int cost) { this->to=to; this->cost=cost; } friend bool operator<(const Edge &a,const Edge &b) { return a.cost < b.cost; } }; vector<Edge> G[MAXN]; int V; int dijkstra(int s) { int d[MAXN]; for(int i=1;i<=MAXN;i++) d[i]=INF; d[s]=0; priority_queue<Edge> que; que.push(Edge(s,0)); while(!que.empty()) { Edge e=que.top();que.pop(); int v=e.to; if(d[v]<e.cost) continue; for(int i=0;i<G[v].size();i++) { Edge ek=G[v][i]; if(d[ek.to]>d[v]+ek.cost) { d[ek.to]=d[v]+ek.cost; que.push(Edge(ek.to,d[ek.to])); } } } int ans=-1; for(int i=1;i<=V;i++) if(d[i]<INF) ans=max(ans,d[i]); return ans; } int main() { while(scanf("%d",&V)!=EOF) { for(int i=1;i<=V;i++) G[i].clear(); for(int i=1;i<=V;i++) for(int j=1;j<=i;j++) if(i==j){ G[i].push_back(Edge(j,0)); G[j].push_back(Edge(i,0)); } else{ char x[10]; scanf("%s",x); if(x[0]==‘x‘) ; else{ G[j].push_back(Edge(i,atoi(x))); G[i].push_back(Edge(j,atoi(x))); } } printf("%d\n",dijkstra(1)); } return 0; }
/* ford 1502 Accepted 444K 0MS G++ */ #include"cstdio" #include"cstring" #include"algorithm" using namespace std; const int MAXN=10005; const int INF=0X3fffffff; struct Edge{ int from,to,cost; }es[MAXN]; int V,E; int ford(int s) { int d[MAXN]; for(int i=1;i<=V;i++) d[i]=INF; d[s]=0; while(true) { bool update=false; for(int i=0;i<E;i++) { Edge e=es[i]; if(d[e.from]!=INF&&d[e.to]>d[e.from]+e.cost) { d[e.to]=d[e.from]+e.cost; update=true; } } if(!update) break; } int ans=-1; for(int i=1;i<=V;i++) { if(d[i]<INF) ans=max(ans,d[i]); } return ans; } int main() { while(scanf("%d",&V)!=EOF) { E=0; for(int i=1;i<=V;i++) for(int j=1;j<i;j++) { char x[10]; scanf("%s",x); if(x[0]!=‘x‘) { es[E].from=i,es[E].to=j,es[E++].cost=atoi(x); es[E].from=j,es[E].to=i,es[E++].cost=atoi(x); } } printf("%d\n",ford(1)); } return 0; }
/* spfa 1502 Accepted 660K 0MS G++ */ #include"cstdio" #include"queue" #include"algorithm" #include"vector" using namespace std; const int MAXN=105; const int INF=0X3fffffff; vector<int> G[MAXN]; int mp[MAXN][MAXN]; int V; int spfa(int s) { int d[MAXN]; int vis[MAXN]; for(int i=1;i<=V;i++) { vis[i]=0; d[i]=INF; } queue<int> que; d[s]=0,que.push(s),vis[s]=1; while(!que.empty()) { int v=que.front();que.pop(); vis[v]=0; for(int i=0;i<G[v].size();i++) { int to=G[v][i]; if(d[to]>d[v]+mp[v][to]) { d[to]=d[v]+mp[v][to]; que.push(to); vis[to]=0; } } } int ans=-1; for(int i=1;i<=V;i++) if(d[i]<INF) ans=max(ans,d[i]); return ans; } int main() { while(scanf("%d",&V)!=EOF) { for(int i=1;i<=V;i++) for(int j=1;j<i;j++) { char x[10]; scanf("%s",x); if(x[0]!=‘x‘) { G[i].push_back(j),G[j].push_back(i); mp[i][j]=mp[j][i]=atoi(x); } } printf("%d\n",spfa(1)); } return 0; }
/* floyd 1502 Accepted 428K 0MS G++ */ #include"cstdio" #include"algorithm" using namespace std; const int MAXN=105; const int INF=0X3fffffff; int mp[MAXN][MAXN]; int V; int main() { while(scanf("%d",&V)!=EOF) { for(int i=1;i<=V;i++) for(int j=1;j<=i;j++) { if(i==j){ mp[i][j]=0; continue; } char x[10]; scanf("%s",x); if(x[0]!=‘x‘) mp[i][j]=mp[j][i]=atoi(x); else mp[i][j]=mp[j][i]=INF; } for(int k=1;k<=V;k++) for(int i=1;i<=V;i++) for(int j=1;j<=V;j++) mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]); int ans=-1; for(int i=1;i<=V;i++) if(mp[1][i]<INF) ans=max(mp[1][i],ans); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5152327.html