文章
1
粉丝
1
获赞
2
访问
128
如果只通过百分之40的数据别忘了这是一个无向图!每条边要加两次!dijkstra的本质就是贪心,每次选距离源点最近的点然后更新边就可以了
#include <bits/stdc++.h>
using namespace std ;
const int N = 1005 ;
int st , ed , n , m ;
int dist[N] , ct[N] ; //存储每个点距离源点距离和花费的最小值
bool vt[N] ; //记录每个点是否被访问过
struct road{
int to , d , cost ;
};
vector<road> edge[N] ; //定义每一个顶点所对应的边表
void dijkstra()
{
memset(dist , 0x3f , sizeof(dist)) ; // 开始时距离和花费都定义为最大
memset(ct , 0x3f , sizeof(ct)) ;
memset(vt , false , sizeof(vt)) ;
dist[st] = 0 ; // 加入源点
ct[st] = 0 ;
for(int i = 0 ; i < n ; i ++ )
{
int t = -1 ;
for(int j = 1 ; j <= n ; j ++ )
{
if(!vt[j] && (t == -1 || dist[j] < dist[t])) // 找出没处理过的且距离出发点最近的点
t = j ;
}
if(t == -1) break ;
vt[t] = true ;
for(int j = 0 ; j < edge[t].size() ; j ++ ) //遍历与点t有关的每一条边
{
int neib = edge[t][j].to ; // 点t的第j条边指向的点
int d = edge[t][j].d ; // t到该点的距离
int c = edge[t][j].cost ; // t到该点的花费
if(dist...
登录后发布评论
暂无评论,来抢沙发