【Dijkstra】【Floyd】-HDU-2544-最水题演示
2014-03-29 02:44
302 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544
题目描述:最裸最短路题,拿来做实验。输入节点数n和路线数m,求最短路
dijkstra+邻接矩阵+优先队列版:
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:
题目描述:最裸最短路题,拿来做实验。输入节点数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; }
相关文章推荐
- HDU 2544 最短路【dijkstra+floyd+spfa+bellman-ford】
- HDU 2544 最短路 Dijkstra 算法、 Floyd 算法 Bellman_ford算法
- HDU2544---最短路(dijkstra&&floyd&&spfa)
- HDU 2544 最短路 dijkstra floyd
- HDU 2544 最短路(dijkstra,Floyd,spfa)
- HDU 2544:最短路( 最短路径入门 &&Dijkstra && floyd )
- HDU 2544 最短路 (Dijkstra || Floyd)
- hdu 2544 最短路 (dijkstra,floyd)
- HDU 2544(Floyd、Dijkstra、Bellman-Ford、SPFA)
- HDU - 2544 - 最短路 (最基础单源最短路问题!!dijkstra+floyd+SPFA)
- hdu 2544 最短路 (dijkstra,floyd)
- HDU 2544 最短路(四种写法:Floyd、Dijkstra、Bellman-Ford、SPFA)
- [dijkstra/SPFA/floyd]HDU 2544最短路
- Floyd Dijkstra Bellman-Ford spfa 四种最短路经典算法汇总 HDU 2544为例
- HDU 2544 最短路(Floyd + Dijkstra + SPFA + 使用链式前向星优化的SPFA + 浅谈最短路)
- HDU 2544 最短路(单源最短路 dijkstra / floyd / dijkstra(堆优化)/dijkstra+邻接链表+堆优化)
- hdu-2544-最短路(Dijkstra + Dijkstra优先队列 + Bellman-ford + SPFA +Floyd) 纯模板题
- HDU 2544-最短路(SPFA,Dijkstra,Floyd)
- HDU-#2544 最短路(Dijkstra、Floyd、Bellman-Ford、SPFA)
- HDU 2544 最短路(Dijkstra和Floyd)