CCF-交通规划

#include<cstdio>
#include<queue>
#include<vector>
#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<Edge> 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<Point> 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;i<G[u].size();i++){
                int 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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值