D - Invitation Cards 最短树 堆结构

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.
Output
For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.
Sample Input

输入由n个事例组成。输入的第一行只包含正整数n,然后按照大小写操作。每种情况都以一行开头,正好包含两个整数p和q,1<=p,q<=1000000。P是包括CCS在内的站点数量,Q是总线数量。然后是Q线,每个Q线描述一条总线。每行仅包含三个数字-起始站、目的站和价格。CCS由数字1指定。价格是正整数,其和小于100000000。你也可以假设从任何一站到另一站都是可能的。

产量

对于每一个案例,打印一行,包含ACM每天支付的志愿者差旅费用的最低金额。

由起点 1 到所有位置的最小和
加上从其他点到1的所有最小和
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include
#include<string.h>
#include
#include
#define ll long long
#define hengheng main ()

const int INF=0x3f3f3f3f;
const int MAX=1e6+5;
using namespace std;
struct node{
int to,len;
int next;
}edge[MAX];
int head[MAX],cnt=0;
void initedge(int n)
{
memset(head+1,-1,sizeof(head));
cnt=0;
}
void addedge(int u,int v,int w)
{
edge[cnt].to=v;
edge[cnt].len=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
struct paidui{
int id;
int len;
};
int n,m;
int dis[1000010],vis[1000010];
bool operator<(paidui a,paidui b)
{
return a.len<b.len;
}

void dijikstra(int S)
{
for(int i=1;i<=n;i++)
dis[i]=INF;
cnt=0;
dis[S]=0;

priority_queue<paidui> q;
q.push({S,0});
while(!q.empty() )
{
	 paidui k=q.top(); q.pop();
   if(k.len!=dis[k.id]) continue;
 
    for(int i=head[k.id];i!=-1;i=edge[i].next)
    {
        int t=edge[i].to;
        if(dis[t]>dis[k.id]+edge[i].len)
        {
            dis[t]=dis[k.id]+edge[i].len;
            q.push(paidui{t,dis[t]});
        }
    }
}

}

int lu1[1000100],lu2[1000100],len[1000100];
int hengheng
{
int t;
scanf("%d",&t);
while(t–)
{

	scanf("%d%d",&n,&m);
	initedge(n);
	for(int i=0;i<m;i++)
	{
		scanf("%d%d%d",&lu1[i],&lu2[i],&len[i]);
		addedge(lu1[i],lu2[i],len[i]);
	}
	dijikstra(1);
	long long sum=0;
	for(int i=1;i<=n;i++)  sum+=dis[i];// 第一个图 
	initedge(n); //初始化 
	for(int i=0;i<m;i++) addedge(lu2[i],lu1[i],len[i]);//第二个图 
	dijikstra(1);
	
	for(int i=1;i<=n;i++)  sum+=dis[i];
	printf("%lld\n",sum);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值