优先队列+邻接链表实现的dijkstra,在判断优先级时增加对该条路径花费的判断,如果最短路径相同,添加最小花费路径。
最开始inf设置为1002导致只得了90,还找了很久的错。细节问题真是要命。
真是力不从心了,写个最短路径都写了一下午,默默吐槽一下自己。。
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#define maxn 10005
#define inf 1<<27
using namespace std;
struct node{
int v,c,p;
node(int vv,int cc,int pp):v(vv),c(cc),p(pp){}
bool operator < (const node & b)const
{
if (c>b.c ||c == b.c && p > b.p){
return true;
}
return false;
};
};
int d[maxn],vis[maxn],cost[maxn];
int n;
vector<node> G[maxn];
int dijkstra(){
priority_queue<node> Q;
memset(vis,0,sizeof(vis));
for(int i = 1;i<=n;i++){
d[i] = (i==1?0:inf);
}
Q.push(node(1,0,0));
int num = 0,ans = 0;
while(!Q.empty()){
node a = Q.top();
Q.pop();
int u = a.v;
if(vis[u]){
cont