五道水题,但要手快才好。。。我手慢了,E题目都没看完TAT....

想了一发,很水,就是一遍Dijk即可,使用优先队列,同时记录由哪条边转移而来

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define LL long long
using namespace std; const int MAXN=; struct Edge{
int u,v,w,i,next;
}edge[MAXN*];
int weight[MAXN],head[MAXN],tot;
int n,m; int chose[MAXN];
LL dis[MAXN]; bool vis[MAXN]; struct Node{
int u; LL d;
Node(){}
Node(int uu,LL dd){u=uu,d=dd;}
bool operator<(const Node &a)const{
return d>a.d;
}
}; vector <int>ans;
priority_queue<Node>pq;
LL cur; void addedge(int u,int v,int w,int i){
edge[tot].u=u; edge[tot].v=v;
edge[tot].w=w; edge[tot].i=i;
edge[tot].next=head[u];
head[u]=tot++;
} void Dijk(int r){
cur=;
for(int i=;i<=n;i++) dis[i]=1ll<<;
dis[r]=;
memset(chose,,sizeof(chose));
memset(vis,false,sizeof(vis));
ans.clear();
pq.push(Node(r,));
while(!pq.empty()){
Node tmp=pq.top(); pq.pop();
if(vis[tmp.u]) continue;
if(tmp.u!=r){
cur+=weight[chose[tmp.u]];
ans.push_back(chose[tmp.u]);
}
LL d=tmp.d;
vis[tmp.u]=true;
for(int e=head[tmp.u];e!=-;e=edge[e].next){
int v=edge[e].v;
if(!vis[v]){
if(d+edge[e].w<dis[v]){
dis[v]=d+edge[e].w;
chose[v]=edge[e].i;
pq.push(Node(v,dis[v]));
}
else if(d+edge[e].w==dis[v]){
if(edge[e].w<weight[chose[v]]){
chose[v]=edge[e].i;
}
}
}
}
}
sort(ans.begin(),ans.end());
cout<<cur<<endl;
int len=ans.size();
if(len>){
printf("%d",ans[]);
for(int i=;i<len;i++)
printf(" %d",ans[i]);
printf("\n");
}
} int main(){
int u,v,w;
while(scanf("%d%d",&n,&m)!=EOF){
memset(head,-,sizeof(int)*(n+));
tot=;
weight[]=1e9+;
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w,i);
addedge(v,u,w,i);
weight[i]=w;
}
scanf("%d",&u);
Dijk(u);
}
return ;
}

Codeforces Round #303 (Div. 2) E的更多相关文章

  1. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  2. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  3. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  4. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  5. Codeforces Round #303 (Div. 2) D. Queue 傻逼题

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

  6. Codeforces Round #303 (Div. 2) C. Woodcutters 贪心

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

  7. Codeforces Round #303 (Div. 2) B. Equidistant String 水题

    B. Equidistant String Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/54 ...

  8. Codeforces Round #303 (Div. 2) A. Toy Cars 水题

     A. Toy Cars Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/problem ...

  9. 「日常训练」Queue(Codeforces Round 303 Div.2 D)

    简单到让人不敢相信是D题,但是还是疏忽了一点. 题意与分析 (Codeforces 545D) 题意:n人排队,当一个人排队的时间超过他需要服务的时间就会厌烦,现在要求一个最优排列使得厌烦的人最少. ...

  10. 「日常训练」Woodcutters(Codeforces Round 303 Div.2 C)

    这题惨遭被卡..卡了一个小时,太真实了. 题意与分析 (Codeforces 545C) 题意:给定\(n\)棵树,在\(x\)位置,高为\(h\),然后可以左倒右倒,然后倒下去会占据\([x-h,x ...

随机推荐

  1. VUE修改每个页面title

    //index.js routes: [ { name:'home', path: '/home/:openname', component: Home, meta: { title: '首页' } ...

  2. dropdownlist显示树形结构

    /// <summary> /// 递归 /// </summary> /// <param name="deplist"></param ...

  3. Server Tomcat v8.0 Server at localhost failed to start 问题解决方法?

    bi编程jsp  servlet 第一个程序: HelloServlet 运行错误 404: 十月 28, 2017 11:25:14 上午 org.apache.tomcat.util.digest ...

  4. 1B课程笔记分享_StudyJams_2017

    课程1B 概述 课程1B主要讲解了Android UI的ViewGroups(视图组).LinearLayout(线性布局).RelativeLayout(相对布局),Portrait Mode(竖屏 ...

  5. win32窗口映射(部分)

    先理解一下“窗口”与“视区”的概念.“窗口”是逻辑坐标下的矩形区域,“视区”是设备坐标系下的区域.根据“窗口”和“视区”的大小可以确定x方向和y方向的比例因子. 例子如下: VOID OnPaint( ...

  6. pycuda installation error: command 'gcc' failed with exit status 1

    原文:python采坑之路 Setup script exited with error: command 'gcc' failed with exit status 1 伴随出现"cuda ...

  7. android Adapter总结

    1.连接视图与数据.数据封装: 2.视图构造与配置: 3.数据更新监听: 4.视图重用机制. * An Adapter object acts as a bridge between an {@lin ...

  8. 解决 i5 6500 安装黑苹果 Sierra 显卡不正常问题

    i5 6500内置HD 530显卡,装好Sierra显卡驱动不太正常. 先下载Clover configurator 用Clover configurator加载 EFI (Mount EFI)分区 ...

  9. ionic下拉多项选择

    1.npm install ion-multi-picker --save 2.引入 import { MultiPickerModule } from 'ion-multi-picker'; imp ...

  10. PAT_A1107#Social Clusters

    Source: PAT A1107 Social Clusters (30 分) Description: When register on a social network, you are alw ...