因为要求是在保证最短路的情况下花费是最小的,所以(先保证最短路设为S吧)

那么花费就是最短路上的新建边条数A+剩余拆掉边的条数B,而且总的原有好的边是一定的,所以,只要使得A尽量小,那么B就大,所以要拆掉的边也会少啦。

所以SPFA以最短路为基础,维护出一个A最小就好。(路径什么的,,from[...])

 #include<bits/stdc++.h>
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define LL long long
#define N 100005
using namespace std;
inline LL ra()
{
LL x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
struct node{
int from,to,next,z;
}e[N<<];
int sum[N<<],dis[N<<],head[N<<],cnt;
int q[N<<],tot1,n,m,from[N<<];
bool vis[N<<],inq[N<<];
void insert(int x, int y, int z)
{
e[cnt].to=y;
e[cnt].from=x;
e[cnt].next=head[x];
e[cnt].z=z;
head[x]=cnt++;
}
void SPFA()
{
for (int i=; i<=n; i++) sum[i]=dis[i]=inf;
sum[]=dis[]=; q[]=; int l=,r=; from[]=-;
while (l<=r)
{
int x=q[l++];
for (int i=head[x];i!=-;i=e[i].next)
{
if (dis[e[i].to]==dis[x]+ && e[i].z==)
{
if (sum[e[i].to]>sum[x]+)
{
sum[e[i].to]=sum[x]+;
from[e[i].to]=i;
if (!inq[e[i].to])
{
q[r++]=e[i].to;
inq[e[i].to]=;
}
}
}
if (dis[e[i].to]==dis[x]+ && e[i].z)
{
if (sum[e[i].to]>sum[x])
{
sum[e[i].to]=sum[x];
from[e[i].to]=i;
if (!inq[e[i].to])
{
q[r++]=e[i].to;
inq[e[i].to]=;
}
}
}
if (dis[e[i].to]>dis[x]+)
{
dis[e[i].to]=dis[x]+;
from[e[i].to]=i;
if (e[i].z==) sum[e[i].to]=sum[x]+;
else sum[e[i].to]=sum[x];
if (!inq[e[i].to])
{
inq[e[i].to]=;
q[r++]=e[i].to;
}
}
}
inq[x]=;
}
// cout<<dis[n];while (1);
}
void print()
{
cout<<sum[n]+(tot1-(dis[n]-sum[n]))<<endl;
int i=from[n];
while (i!=-)
{
if (e[i].z==)
printf("%d %d 1\n",e[i].from,e[i].to);
else vis[i]=;
i=from[e[i].from];
}
for (int i=; i<=cnt; i++)
{
if (e[i].z== && vis[i]== && vis[i^]==)
{
vis[i]=;
printf("%d %d 0\n",e[i].from,e[i].to);
}
}
}
int main()
{
n=ra(); m=ra();
memset(head,-,sizeof(head));
for (int i=; i<=m; i++)
{
int x=ra(),y=ra(),z=ra();
if (z==) tot1++;
insert(x,y,z); insert(y,x,z);
}
SPFA();
print();
return ;
}

cf 507E. Breaking Good的更多相关文章

  1. CodeForces 507E Breaking Good 2维权重dij

    Breaking Good 题解:         2维权重dij, 先距离最短, 后改变最小.   在这个题中, 如果要改变最小, 则让更多的可用边放进来. 然后可以用pre存下关键边.   代码: ...

  2. [Codeforces 507E] Breaking Good

    [题目链接] https://codeforces.com/contest/507/problem/E [算法] 首先BFS求出1到其余点的最短路 , N到其余点的最短路,记为distA[]和dist ...

  3. 【codeforces 507E】Breaking Good

    [题目链接]:https://vjudge.net/contest/164884#problem/D [题意] 给你一张图; 图中有些路是完好的;但有些路还没修好; 先不管路有没有修好; 问你从起点到 ...

  4. CF习题集二

    CF习题集二 一.CF507E Breaking Good 题目描述 \(Breaking Good\)这个游戏对于有经验的玩家来说也有一定的难度. 游戏的主角小明希望加入一个叫斧头帮的犯罪团伙.这个 ...

  5. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  6. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  7. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  8. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  9. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

随机推荐

  1. JDK8中的ConcurrentHashMap源码

    背景 上文JDK8中的HashMap源码写了HashMap,这次写ConcurrentHashMap ConcurrentHashMap源码 /** * Maps the specified key ...

  2. 085、Java数组之实现排序

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  3. VMware导入和删除虚拟机文件

    VMware中导入已存在的虚拟机文件: 1.文件-->打开-->选择虚拟机文件-->完成     VMware中完全删除虚拟机文件 2.在虚拟机上右键单击-->管理--> ...

  4. Day6 - 牛客203E

    https://ac.nowcoder.com/acm/contest/203/E 埋坑不会做

  5. 习题两则的简化(利用for循环)

    习题一.打印26个英文字母 public class PrintChars { public static void main(String[] args) { char ch = 'a'; int ...

  6. 7.4 Varnish VCL的子程序

  7. 树莓派 Raspberry 软件源更改 看门狗启用

    看门狗无法在pi1上执行,似乎后更高级的pi上面才可用 1.替换脚本 下面脚本请直接复制到终端执行!! 适用于raspbian-stretch(基于Debian9) sudo -s echo -e & ...

  8. 微信浏览器 UA

    mozilla/5.0 (linux; android 5.1.1; mi note pro build/lmy47v) applewebkit/537.36 (khtml, like gecko) ...

  9. CSS隐藏商务通等内容

    CSS隐藏商务通等内容<style>#qiao-wrap{display:none !important;} </style>

  10. 【pwnable.kr】 memcpy

    pwnable的新一题,和堆分配相关. http://pwnable.kr/bin/memcpy.c ssh memcpy@pwnable.kr -p2222 (pw:guest) 我觉得主要考察的是 ...