【题目链接】:https://vjudge.net/contest/164884#problem/D

【题意】



给你一张图;

图中有些路是完好的;但有些路还没修好;

先不管路有没有修好;

问你从起点到终点的最短路;

如果最短路上有没修好的路,那么你要把它修好;

而不在最短路上的,如果是完好的路,你需要把它摧毁(???)

让你选出这么一条最短路,使得受影响的路(被摧毁的和修好的路的数目总和)最少;

【题解】



受影响的路即是:

最短路上的没修好的路+非最短路上的修好的路;

也即;

最短路的长度-最短路上的修好的路+整张图上修好的路-最短路上修好的路

也即

最短路的长度+整张图上修好的路-2*最短路上修好的路;

要让受影响的路最少;

即让最短路上修好的路最大就好;

这个可以在做spfa的时候顺便得到;

即最短路相同,再判断一下修好的路的个数;

按照那个公式算出受影响的路;

然后记录每个点的前缀;

最短路上没修好的路把它修好(输出);

不是最短路上的修好的路把它破坏(输出);



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); const int N = 1e5+50;
const int INF = 0x3f3f3f3f; struct abc{
int x,y,z;
}; int n,m,all1,k;
abc a[N];
vector <pii> G[N];
pii dis[N],pre[N];
queue <int> dl;
bool inq[N],bo[N]; void spfa()
{
rep1(i,1,n)
dis[i] = mp(INF,0);
dis[1].fi = 0;
dl.push(1);
inq[1] = true;
while (!dl.empty())
{
int x = dl.front();
dl.pop();
inq[x] = false;
for (pii temp:G[x])
{
int y = temp.fi,w = a[temp.se].z;
if (dis[y].fi>dis[x].fi+1 || (dis[y].fi==dis[x].fi+1 && dis[y].se<dis[x].se+w))
{
dis[y].fi = dis[x].fi+1;
dis[y].se = dis[x].se+w;
pre[y] = mp(x,temp.se);
if (!inq[y])
{
inq[y] = true;
dl.push(y);
}
}
}
}
} int main()
{
//Open();
cin >> n >> m;
rep1(i,1,m)
{
cin >> a[i].x >> a[i].y >> a[i].z;
G[a[i].x].pb(mp(a[i].y,i));
G[a[i].y].pb(mp(a[i].x,i));
all1+=a[i].z;
}
spfa();
k = dis[n].fi+all1-2*dis[n].se;
cout << k << endl;
int now = n;
while (now!=1)
{
int temp = pre[now].se;
if (a[temp].z==0)
cout << a[temp].x <<' '<<a[temp].y<<' '<<1<<endl;
bo[temp] = 1;
now = pre[now].fi;
}
rep1(i,1,m)
if (!bo[i] && a[i].z==1)
cout << a[i].x <<' '<<a[i].y<<' '<<0<<endl;
return 0;
}

【codeforces 507E】Breaking Good的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. react 实现类似vue中的<keep-alive>的功能,并解决antd-mobile切换回来时的空白

    在移动端的spa页面中,只要使用到了路由就很有必要使用到状态保存的功能,这样才能保证在页面进行切换的时候,让用户可以看到刚才滑动的地方,让用户的体验更加友好.这儿我找到了react-router-ca ...

  2. 「题解」:$d$

    问题 A: $d$ 时间限制: 1 Sec  内存限制: 512 MB 题面 题面谢绝公开. 题解 赛时切掉了然而过程十分曲折. 贪心思路很好想.然而一开始错误以为是单峰.其实几个峰都有可能. 开场写 ...

  3. LUOGU P1039 侦探推理 (字符串+模拟)

    传送门 解题思路 一道%你神题,\(string\)好强大啊..首先枚举一个周几,再枚举一个罪犯是谁,然后判断的时候就是枚举所有人说的话.定义\(fAKe[i]\)表示第\(i\)个人说的是真话还是假 ...

  4. LUOGU P2290 [HNOI2004]树的计数(组合数,prufer序)

    传送门 解题思路 \(prufer\)序,就是所有的不同的无根树,都可以转化为唯一的序列.做法就是每次从度数为\(1\)的点中选出一个字典序最小的,把这个点删掉,并把这个点相连的节点加入序列,直到只剩 ...

  5. 牛客多校第五场 A digits 2 签到

    题意: 给定一个n,输出一个数,要求这个数所有位之和整除n,并且这个数也整除n,并且位数不许多于1e4 题解: 把这个数n输出n遍. #include<iostream> using na ...

  6. VS中检测数据库链接

    在程序中链接数据库,总要为链接语句发愁.可以尝试在链接前,从VS中测试下链接,测试成功的话,可以直接将链接语句复制到程序中. 在VS中,选择“工具”——“连接到数据库”,如下:

  7. pycharm同时使用python2.7和python3.5设置方法

    pycharm同时使用python2.7和python3.5设置方法 - CSDN博客https://blog.csdn.net/qwerty200696/article/details/530159 ...

  8. more 和less 命令简单介绍以及使用

    一.more命令 more功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 ...

  9. 2019-8-31-PowerShell-通过-WMI-获取系统安装软件

    title author date CreateTime categories PowerShell 通过 WMI 获取系统安装软件 lindexi 2019-08-31 16:55:58 +0800 ...

  10. 一个页面上调用多个setInterval失效解决办法(使用于同一时间间隔)

    将方法封装在一起,只调用一个setInterval window.setInterval( function () { $.ajax({ type: "GET", url: '/M ...