本文主要是介绍HDU 2544 最短路 (多种解法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最短路
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 35912 Accepted Submission(s): 15609
输入保证至少存在1条商店到赛场的路线。
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
const ull inf = 1LL << 61;
const double eps=1e-5;
const int INF=1<<30;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int edge[110][110];
int lowcost[110];
int vis[110];
int n,m;
void Dj(int cur)
{
cle(vis);
vis[cur]=1;
for(int i=1;i<=n;i++)
lowcost[i]=edge[cur][i];
int k,Min;
while(1)
{
Min=INF;
for(int j=1;j<=n;j++)
if(!vis[j]&&Min>lowcost[j])
{
Min=lowcost[j];k=j;
}
if(Min==INF)break;
vis[k]=1;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&lowcost[j]>lowcost[k]+edge[k][j])
lowcost[j]=lowcost[k]+edge[k][j];
}
}
cout<<lowcost[n]<<endl;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int a,b,c;
while(cin>>n>>m)
{
if(n==0&&m==0)break;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{ if(i==j){edge[i][j]=0;edge[j][i]=0;}
else {edge[i][j]=INF;edge[j][i]=INF;}
}
for(int i=1;i<=m;i++)
{
cin>>a>>b>>c;
if(c<INF)
{ edge[a][b]=c;
edge[b][a]=c;
}
}
// cout<<INF<<endl;
Dj(1);
}
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
const ull inf = 1LL << 61;
const double eps=1e-5;
const int INF=99999999;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int d[110][110];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int i,j,k,m,n;
int a,b,c;
while(cin>>n>>m)
{
if(n==0&&m==0)break;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{d[i][j]=INF;d[j][i]=INF;}
d[i][i]=0;///注意初始化细节
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(d[a][b]>c)
{
d[a][b]=c;d[b][a]=c;
}
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(d[i][k]+d[k][j]<d[i][j])
d[i][j]=d[i][k]+d[k][j];
}
cout<<d[1][n]<<endl;
}
return 0;
}
Bellman-Ford算法解法
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
const ull inf = 1LL << 61;
const double eps=1e-5;
const int INF=99999999;
using namespace std;
bool cmp(int a,int b){
return a>b;
}
struct Edge
{
int u,v;
int weight;
}edge[2*maxn]; ///保存边的值
int dist[110]; ///节点到源点的最小距离
int nodenum,edgenum,source=1;
int a,b,c;
void init()
{
for(int i=1;i<=nodenum;i++)
dist[i]=INF;
dist[source]=0;
for(int i=1;i<=2*edgenum;i++)
{
cin>>a>>b>>c;///无向图
edge[i].u=a;edge[i].v=b;edge[i].weight=c;
///if(edge[i].u==source)如果起点和为原点
///dist[edge[i].v]=edge[i].weight;
i++;
edge[i].u=b;edge[i].v=a;edge[i].weight=c;
}
}
///松弛操作
void relax(int u,int v,int weight)
{
if(dist[v]>dist[u]+weight)
{
dist[v]=dist[u]+weight;
}
}
bool Bellman_Ford()
{
for(int i=1;i<nodenum;i++)///枚举边,1条边到终点2条边到终点.....
for(int j=1;j<=2*edgenum;j++)
relax(edge[j].u,edge[j].v,edge[j].weight);
bool flag=1;
///判断是否有负环路
///无向图!!
for(int i=1;i<=edgenum;i++)
if(dist[edge[i].v]>dist[edge[i].u]+edge[i].weight)///还可以更新的话
{
flag=0;
break;
}
return flag;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>nodenum>>edgenum)
{
if(nodenum==0&&edgenum==0)break;
init();
if(Bellman_Ford())
cout<<dist[nodenum]<<endl;
}
return 0;
}
这篇关于HDU 2544 最短路 (多种解法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!