CCF-交通规划

#include
#include
#include
#define maxn 100005
#define inf 0x7fffffff  
int n,m;
using namespace std;
struct Point
{
    int u;
    int dist;
    Point(int uu,int d){
        u=uu,dist=d;
    }
    friend bool operator < (Point a,Point b) {
        return a.dist > b.dist;  
    }
};
struct Edge
{
    int v;
    int cost;
    Edge(int vv,int c){
        v=vv,cost=c;
    }
};
vector G[maxn];
bool vis[maxn];
int disto[maxn];
int costo[maxn];

void Dijkstra(int s)
{
    for(int i=1;i<=n;i++){
        vis[i]=false;
        disto[i]=costo[i]=inf;
    }
    disto[s]=0;
    costo[s]=0;
    priority_queue queue;
    queue.push(Point(s,0));
    while(!queue.empty()){
        Point p=queue.top();
        queue.pop();
        int u=p.u;
        if(!vis[u]){
            for(int i=0;iint v=G[u][i].v;
                int co=G[u][i].cost;
                if(!vis[v]){
                    if(disto[v]>disto[u]+co){
                        disto[v]=disto[u]+co;
                        queue.push(Point(v,disto[v]));
                        costo[v]=co;
                    }
                    if(disto[v]==disto[u]+co){
                        costo[v]=min(costo[v],co);
                    }
                }
            }
        }
    }
}
int main()
{
    int u,v,c;
    scanf("%d%d",&n,&m);
    while(m--){
        scanf("%d%d%d",&u,&v,&c);
        G[u].push_back(Edge(v,c));
        G[v].push_back(Edge(u,c));
    }
    Dijkstra(1);
    int ans=0;
    for(int i=2;i<=n;i++)
        ans+=costo[i];
    printf("%d",ans);
}

你可能感兴趣的:(迪杰斯特拉)