生活随笔
收集整理的這篇文章主要介紹了
【codeforces 507E】Breaking Good
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【題目鏈接】: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()
{
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;
}
轉載于:https://www.cnblogs.com/AWCXV/p/7626299.html
總結
以上是生活随笔為你收集整理的【codeforces 507E】Breaking Good的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。