完全模拟优先队列实现dijskal最短路。
代码:
//#include<bits/stdc++.h>
//using namespace std;
//using ll=long long ;
//const int inf=0x3f3f3f3f;
//int dp[400][400];
//char S[400],T[400];
//int main()
//{
// int a,b,c,d;
// while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
// {
// scanf("%s",S+1);
// scanf("%s",T+1);
// int s=strlen(S+1),t=strlen(T+1);
//// S[s+1]='a',T[s+1]='a';
//// cout<<S[1]<<endl;
// for(int i=0; i<=max(s,t); i++)
// dp[i][0]=-i*d,dp[0][i]=-i*c;
// for(int i=1; i<=s; i++)
// {
// for(int j=1; j<=t; j++)
// {
// dp[i][j]=-inf;
// for(int k=0; k<=i; k++)
// {
// if(k!=i)
// {
// if(S[i]==T[j])
// dp[i][j]=max(dp[i][j],dp[k][j-1]-(i-k-1)*d+a);
// else
// dp[i][j]=max(dp[i][j],dp[k][j-1]-(i-k-1)*d-b);
// }
// else
// dp[i][j]=max(dp[i][j],dp[k][j-1]-c);
// }
// for(int k=0; k<=j; k++)
// {
// if(k!=j)
// {
// if(S[i]==T[j])
// dp[i][j]=max(dp[i][j],dp[i-1][k]-(j-k-1)*c+a);
// else
// dp[i][j]=max(dp[i][j],dp[i-1][k]-(j-k-1)*c-b);
// }
// else
// dp[i][j]=max(dp[i][j],dp[i-1][k]-d);
// }
// }
// }
// int ans=-inf;
// for(int i=1; i<=s; i++)
// {
// dp[i][t]-=(s-i)*d;
// ans=max(dp[i][t],ans);
// }
// printf("%d\n",ans);
// }
//}
//
#include<bits/stdc++.h>
#include<stdio.h>
#include <algorithm>
#include <iostream>
#include <stdio.h>
//using namespace std;
//int main()
//{
// int data[122] = {1, 2, 3, 4,5,6,7,8,9,10,11,12,13};
//// int t=lower_bound(data,data+4,0)-data;
//// cout<<t<<endl;
// for(int k=6; k<13; k++)
// {
// cout<<k<<" ";
// int cnt = 0;
// do
// {
// int s[100],ss[100];
// int xx=1;
// for(int i=0; i<k; i++)
// ss[i]=data[i];
// sort(ss,ss+5);
// s[0]=ss[0];
// for (int i = 1; i < k; ++i)
// {
// int t=lower_bound(s,s+xx,ss[i])-s;
//// cout<<t<<endl;
// s[t]=ss[i];
// if(t==xx)
// xx++;
// }
//// break;
// if (xx>=k-1)
// cnt++;
// }
// while (next_permutation(data, data + k));
// cout << cnt << endl;
// }
// return 0;
//}
//
#include <iostream>
#include <fstream>
#include <vector>
//#include<bits/stdc++.h>
using namespace std;
const int inf =0x3f3f3f3f;
class n_node //stand for neighbor_node
{
public:
int id; //neighbor id
int weight; //edge weight
//Do your own constructor(s)
};
class rt_node
{
public:
bool is_visited; //true if visited; else false
int cost; //path cost
int from; //from node
int prenode;
// int h_pos;//the positon of this node in heap
//Do your own constructor(s)
};
class h_node
{
public:
int id;//node id
int cost; //cost to this node from source
//Do your own constructor(s)
};
vector < vector<n_node>* > Graph;
vector <rt_node> Routing_Table;
vector <h_node> Heap;
void adjustdown(vector<h_node>&S,int k,int len)
{
S[0]=S[k];
for(int i=2*k; i<=len; i*=2)
{
if(i<len&&S[i].cost>S[i+1].cost)
i++;
if(S[0].cost<=S[i].cost)
break;
else
{
S[k]=S[i];
k=i;
}
}
S[k]=S[0];
}
void bulidmaxheap(vector<h_node>&S,int len)
{
for(int i=len/2; i>=0; i--)
{
adjustdown(S,i,len);
}
}
void heapsort(vector<h_node>&S,int len)
{
bulidmaxheap(S,len);
for(int i=len; i>=1; i--)
{
swap(S[i],S[1]);
adjustdown(S,1,i-1);
}
}
void adjustup(vector<h_node>&S, int k)
{
S[0]=S[k];
int i=k/2;
while(i>0&&S[i].cost>S[0].cost)
{
S[k]=S[i];
k=i;
i=k/2;
}
S[k]=S[0];
// printf("k==%d %d\n",k,S[k].id);
}
void dijkstra(int n,int s)
{
for(int i=0; i<n; i++)
{
rt_node b;
b.is_visited=false;
b.cost=inf;
if(i==s)
b.cost=0;
b.from=i;
b.prenode=i;
Routing_Table.push_back(b);
}
Heap.clear();
h_node D;
D.id=s;
D.cost=0;
Heap.push_back(D);
Heap.push_back(D);
bulidmaxheap(Heap,Heap.size()-1);
// heapsort(Heap,Heap.size()-1);
while(Heap.size()>1)
{
//printf("dfsaf\n");
D=Heap[1];
// cout<<"fsdgfds"<<D.id<<" "<<D.cost<<endl;
Heap[1]=Heap[Heap.size()-1];
// cout<<"12346"<<Heap[1].id<<" "<<Heap[1].cost<<endl;
if(Heap.size()-2>1)
adjustdown(Heap,1,Heap.size()-2);
Heap.erase(Heap.begin()+Heap.size()-1);
// cout<<"12234455"<<Heap.size()<<endl;
int u=D.id,flag=0,biao1,biao2,j=0;
for(auto it=Routing_Table.begin(); it!=Routing_Table.end(); it++,j++)
{
if(it->from==u)
{
if(it->is_visited==false)
{
it->is_visited=true;
flag=1;
biao1=j;
break;
}
}
}
if(flag==0)
continue;
for(int i=0; i<Graph[0][u].size(); i++)
{
int v=Graph[0][u][i].id;
int cost=Graph[0][u][i].weight,jj=0;
flag=0;
for(auto it=Routing_Table.begin(); it!=Routing_Table.end(); it++,jj++)
{
if(it->from==v)
{
if(it->is_visited==false)
{
// it->is_visited=true;
biao2=jj;
flag=1;
break;
}
}
}
if(flag==0)
continue;
if(Routing_Table[jj].cost>Routing_Table[j].cost+cost)
{
// printf("dfsaf\n");
Routing_Table[jj].cost=Routing_Table[j].cost+cost;
Routing_Table[jj].prenode=j;
// cout<<jj <<" "<<j<<" "<<Routing_Table[jj].cost<<endl;
h_node K;
K.id=v;
K.cost=Routing_Table[jj].cost;
Heap.push_back(K);
adjustup(Heap,Heap.size()-1);
// for(int k=1; k<Heap.size(); k++)
// {
// cout<<"fdsvbfd"<<Heap[k].id<<" "<<Heap[k].cost<<endl;
// }
}
}
}
}
int main()
{
int s;
int n;
int m;
scanf("%d%d%d",&s,&n,&m);
vector<n_node>T[n+1];
for(int i=0; i<m; i++)
{
int x;
n_node w;
scanf("%d%d%d",&x,&w.id,&w.weight);
// if(w.id==1)
// cout<<x<<"qwerrrrr"<<endl;
T[x].push_back(w);
}
Graph.push_back(T);
dijkstra( n, s);
for(int i=0; i<Routing_Table.size(); i++)
{
if(i==s)
continue;
printf("The cost from node %d to node %d is: %d from node is %d\n",s,Routing_Table[i].from,Routing_Table[i].cost,Routing_Table[i].prenode);
}
}