蒟蒻求助TLE

P1342 请柬

Manjusaka丶梦寒 @ 2018-09-27 19:50:16

dalao帮忙看看,前三个点都T,谢谢。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#define LL long long
using namespace std;
#define LL long long
LL n,m,s,ans;
typedef pair<LL,int> pii;
struct ahah{
    int nxt,to,dis;
}edge1[1000010],edge2[1000006];
int head1[1000010],tot1,head2[1000006],tot2;
void add(int x,int y,int z,int flag)
{
    if(flag==1)edge1[++tot1].nxt=head1[x],edge1[tot1].to=y,edge1[tot1].dis=z,head1[x]=tot1;
    else edge2[++tot2].nxt=head2[x],edge2[tot2].to=y,edge2[tot2].dis=z,head2[x]=tot2;
}
priority_queue <pii,vector<pii>,greater<pii> >Q;
bool vis[1000010];
LL d[1000010];
void dijkstra1(int s)
{
    for(int i=1;i<=n;i++)d[i]=1e15;
    Q.push(make_pair(0,s));
    d[s]=0;
    while(!Q.empty())
    {
        while(!Q.empty()&&vis[Q.top().second])Q.pop();
        if(Q.empty())return ;
        int temp=Q.top().second;
        d[temp]=Q.top().first;
        vis[temp]=1;Q.pop();
        for(int i=head1[temp];i;i=edge1[i].nxt)
        {
            if(!vis[edge1[i].to])Q.push(make_pair(d[temp]+edge1[i].dis,edge1[i].to));
        }
    }
    return ;
}
void dijkstra2(int s)
{
    for(int i=1;i<=n;i++)d[i]=1e15;
    Q.push(make_pair(0,s));
    d[s]=0;
    while(!Q.empty())
    {
        while(!Q.empty()&&vis[Q.top().second])Q.pop();
        if(Q.empty())return ;
        int temp=Q.top().second;
        d[temp]=Q.top().first;
        vis[temp]=1;Q.pop();
        for(int i=head2[temp];i;i=edge2[i].nxt)
        {
            if(!vis[edge2[i].to])Q.push(make_pair(d[temp]+edge2[i].dis,edge2[i].to));
        }
    }
    return ;
}
main()
{
    int x,y,z;
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z,1);add(y,x,z,2);
    }
    dijkstra1(1);
    for(int i=2;i<=n;i++)ans+=d[i];
    memset(vis,0,sizeof(vis));
    dijkstra2(1);
    for(int i=2;i<=n;i++)ans+=d[i];
    printf("%lld",ans);
}

by 清风我已逝 @ 2018-09-27 20:04:37

@Manjusaka丶梦寒 有可能你脸黑


by 清风我已逝 @ 2018-09-27 20:36:01

你个垃圾,STL太慢啦


by ecnerwaIa @ 2018-10-16 10:03:13

我为什么dijkstraWA了!!


|