第一次的代码是我以前打的,是100分
#include <bits/stdc++.h>
using namespace std;
#define N 10001
#define M 500001
#define P pair<int,int>
#define Fi first
#define Se second
struct node{
int to,w,next;
}edge[M];
int cut=0;
int head[N];
int dis[N];
int n,m,k;
bool vis[N];
priority_queue<P,vector<P>,greater<P> > q;
void ini(){
fill(dis+1,dis+N+1,INT_MAX);
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w){
cut++;
edge[cut].w=w;
edge[cut].to=v;
edge[cut].next=head[u];
head[u]=cut;
}
void Dij(int x){
dis[x]=0;
vis[x]=1;
q.push(P(0,x));
while(!q.empty()){
P u=q.top();q.pop();vis[u.Se]=1;
for(int j=head[u.Se];~j;j=edge[j].next){
if(!vis[edge[j].to]&&dis[edge[j].to]>dis[u.Se]+edge[j].w){
dis[edge[j].to]=dis[u.Se]+edge[j].w;
q.push(P(dis[edge[j].to],edge[j].to));
}
}
}
}
int main(){
ini();
scanf("%d %d %d",&n,&m,&k);
for(int i=1,u,v,w;i<=m;i++){
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
Dij(k);
for(int i=1;i<=n;i++){
printf("%d ",dis[i]);
}
return 0;
}
后来又打了一次,感觉没什么区别,却只有30分
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#define P pair <int, int>
using namespace std;
template <typename T> inline void fnRead (T& m_In) {
m_In = 0;
int m_Flag = 1;
char m_Ch = getchar ();
for (; ! isdigit (m_Ch); m_Ch = getchar ()) if (m_Ch == '-') m_Flag = -1;
for (; isdigit (m_Ch); m_Ch = getchar ()) m_In = (m_In << 3) + (m_In << 1) + (m_Ch ^ 48);
m_In *= m_Flag;
}
template <typename T, typename ...Arge> inline void fnRead (T& m_In, Arge&... m_InArge) {
fnRead (m_In);
fnRead (m_InArge...);
}
struct edge {
int m_Next;
int m_To;
int m_Data;
}g_Edge[500100];
int g_Cnt = 0;
int g_Head[100100];
int g_Vis[100100];
int g_Dis[100100];
priority_queue <P, vector <P>, greater<P> > g_Queue;
void fnInit () {
fill (g_Head + 1,g_Head + 110 + 1, -1);
memset (g_Dis, 0x7f, sizeof (g_Dis));
}
void fnAddEdge (int l_U, int l_V, int l_W) {
g_Cnt ++;
g_Edge[g_Cnt].m_Data = l_W;
g_Edge[g_Cnt].m_To = l_V;
g_Edge[g_Cnt].m_Next = g_Head[l_U];
g_Head[l_U] = g_Cnt;
}
void fnDij (int l_Begin) {
g_Dis[l_Begin] = 0;
g_Vis[l_Begin] = 1;
g_Queue.push (P (0, l_Begin));
while (! g_Queue.empty ()) {
P l_Tmp = g_Queue.top ();
g_Queue.pop ();
g_Vis[l_Tmp.second] = 1;
for (int i = g_Head[l_Tmp.second]; ~i; i = g_Edge[i].m_Next) {
if (! g_Vis[g_Edge[i].m_To] && g_Edge[i].m_Data + g_Dis[l_Tmp.second] < g_Dis[g_Edge[i].m_To]) {
g_Dis[g_Edge[i].m_To] = g_Edge[i].m_Data + g_Dis[l_Tmp.second];
g_Queue.push (P (g_Dis[g_Edge[i].m_To], g_Edge[i].m_To));
}
}
}
}
int main () {
fnInit();
int l_PointNumber, l_EdgeNumber, l_Begin;
fnRead (l_PointNumber, l_EdgeNumber, l_Begin);
for (int i = 1, l_U, l_V, l_W; i <= l_EdgeNumber; i ++) {
fnRead (l_U, l_V, l_W);
fnAddEdge (l_U, l_V, l_W);
}
fnDij (l_Begin);
for (int i = 1; i <= l_PointNumber; i ++) {
cout << g_Dis[i] << " ";
}
cout << endl;
return 0;
}
求助