您的位置:首页 > 编程语言 > C语言/C++

【Dijkstra】【Floyd】-HDU-2544-最水题演示

2014-03-29 02:44 302 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544

题目描述:最裸最短路题,拿来做实验。输入节点数n和路线数m,求最短路

dijkstra+邻接矩阵+优先队列版:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;

const int INF = 0x3f3f3f3f;
typedef pair <int,int> pii;

int cost[120][120],dis[120],vis[120];
int n,m;

void dijkstra()
{
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
priority_queue <pii,vector<pii>,greater<pii> > q;

int from=1,to;
dis[from]=0;
q.push(make_pair(dis[from],from));
while(!q.empty())
{
pii t=q.top();q.pop();
from=t.second;
if(vis[from])continue;
vis[from]=1;
for(to=1;to<=n;to++)
{
if(!vis[to]&&dis[from]+cost[from][to]<dis[to]&&cost[from][to]<INF)
{
dis[to]=dis[from]+cost[from][to];
q.push(make_pair(dis[to],to));
}
}
}
}

int main()
{
freopen("input.txt","r",stdin);
int from,to,w;
while(cin>>n>>m,n||m)
{
memset(cost,INF,sizeof(cost));
while(m--)
{
cin>>from>>to>>w;
cost[from][to]=cost[to][from]=min(cost[from][to],w);
}
dijkstra();
cout<<dis
<<endl;
}
return 0;
}





dijkstra+邻接表+优先队列版:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 120;

typedef pair <int,int> pii;

struct edge
{
int end;
int value;
edge(int a,int b){end=a;value=b;}
};

int dis[maxn],vis[maxn];
int n,m;
vector<edge> eg[maxn];

int dijkstra(int S,int T)
{
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
priority_queue <pii,vector<pii>,greater<pii> > q;

int from=S,to,i;
dis[from]=0;
q.push(make_pair(dis[from],from));

while(!q.empty())
{
pii t=q.top();q.pop();
from=t.second;
if(vis[from])continue;
vis[from]=1;
for(i=0;i<eg[from].size();i++)
{
to=eg[from][i].end;
if(!vis[to]&&dis[from]+eg[from][i].value<dis[to])
{
dis[to]=dis[from]+eg[from][i].value;
q.push(make_pair(dis[to],to));
}
}
}
return dis[T];
}

void add_edge(int from,int to,int w)
{
int flag=0,i,j;
for(i=0;i<eg[from].size();i++)
{
flag=0;
if(eg[from][i].end==to)
{
eg[from][i].value=min(eg[from][i].value,w);flag=1;break;
}
}
if(!flag)
{
eg[from].push_back(edge(to,w));
}

for(i=0;i<eg[to].size();i++)
{
flag=0;
if(eg[to][i].end==from)
{
eg[to][i].value=min(eg[to][i].value,w);flag=1;break;
}
}
if(!flag)
{
eg[to].push_back(edge(from,w));
}
}

void edge_test()
{
for(int i=1;i<=n;i++)
{
for(int j=0;j<eg[i].size();j++)
cout<<i<<"-->"<<eg[i][j].end<<"need"<<eg[i][j].value<<endl;
}
cout<<endl;
}

int main()
{
freopen("input.txt","r",stdin);
int i,j,from,to,w;
while(cin>>n>>m,n||m)
{
for(i=0;i<maxn;i++)eg[i].clear();
while(m--)
{
cin>>from>>to>>w;
add_edge(from,to,w);
}
//edge_test();
int ans=dijkstra(1,n);
cout<<ans<<endl;
}
return 0;
}





Floyd:

#include <iostream>
#include <cstdio>
#include <cstring>

const int INF=0x3f3f3f3f;
using namespace std;

int n,m;
int  d[120][120];

int main()
{
int from,to,w,i,j,k;
freopen("input.txt","r",stdin);
while(cin>>n>>m,n||m)
{
memset(d,INF,sizeof(d));
while(m--)
{
cin>>from>>to>>w;
d[from][to]=d[to][from]=min(d[from][to],w);
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
cout<<d[1]
<<endl;
}
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息