_NaCly_Fish @ 2020-02-16 10:51:23
#include<cstdio>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
const int N=1000010;
long long n,m,d[N],ans;
struct edge
{
long long to,cost;
};
vector<edge>G[N];
typedef pair<int,int>P;
void dijkstra(int s)
{
priority_queue<P,vector<P>,greater<P> >q;
for(register int i=1;i<=n;i++) d[i]=0x3f3f3f3f;
d[s]=0;
q.push(P{0,s});
while(!q.empty())
{
P p=q.top();q.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(register int i=0;i<G[v].size();i++)
{
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
q.push(P{d[e.to],e.to});
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(edge{v,w});
}
dijkstra(1);
for(register int i=1;i<=n;i++)
{
ans+=d[i];
}
for(register int i=2;i<=n;i++)
{
dijkstra(i);
ans+=d[1];
}
printf("%d\n",ans);
return 0;
}
by Smile_Cindy @ 2020-02-16 10:56:20
@fangfx
typedef pair<int,int>P;
int?
by _NaCly_Fish @ 2020-02-16 11:06:47
开longlong也是TLE
by _NaCly_Fish @ 2020-02-16 11:07:07
@Alpha
by Smile_Cindy @ 2020-02-16 11:15:58
@fangfx
O,我看明白了,你每个点都跑个Dijkstra不TLE才怪
by Smile_Cindy @ 2020-02-16 11:16:26
n<=1000000
by dmyjqwdy @ 2020-02-16 11:16:40
您最好不要每个点都跑一边Dijkstra,这种题目一般的都要反向建边,跑两次。
by _NaCly_Fish @ 2020-02-16 13:01:50
请问怎么建翻边 @shenlehan233 @Alpha
by Smile_Cindy @ 2020-02-16 13:02:58
@fangfx
读入的时候建啊?
by _NaCly_Fish @ 2020-02-16 13:06:58
G[v].push_pack(edge{u,w});?
by dmyjqwdy @ 2020-02-16 15:32:04
其实是另外建一个图。这个图里存的是反向边。