Codeforces Round #287 (Div. 2) E. Breaking Good 最短路
题目链接:
http://codeforces.com/problemset/problem/507/E
E. Breaking Good
memory limit per test256 megabytes
样例输出
3
2 3 0
1 5 0
6 8 1
题意
给你n个点,m条边的图,每条边长度都为1,如果标记为0,则这条边待修,1则可正常使用,现在让你找一条最短路,路径上面的待修理的边最少。
题解
跑完最短路之后建最短路构成的DAG图,然后跑拓扑排序跑dp。
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e5+10;
struct Edge{
int u,v,z;
Edge(int u,int v,int z):u(u),v(v),z(z){}
};
struct Spfa{
int n,m;
vector<Edge> egs;
VI G[maxn],G2[maxn];
bool inq[maxn];
int d[maxn],d2[maxn];
void init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
egs.clear();
}
void addEdge(int u,int v,int z){
egs.pb(Edge(u,v,z));
m=egs.sz();
G[u].pb(m-1);
}
int spfa(int s,int *d){
queue<int> Q;
clr(inq,0);
rep(i,0,n) d[i]=INF;
d[s]=0,inq[s]=true,Q.push(s);
while(!Q.empty()){
int u=Q.front(); Q.pop();
inq[u]=false;
for(int i=0;i<G[u].sz();i++){
Edge& e=egs[G[u][i]];
if(d[e.v]>d[u]+1){
d[e.v]=d[u]+1;
if(!inq[e.v]){
Q.push(e.v);
inq[e.v]=true;
}
}
}
}
}
int ind[maxn],dp[maxn];
int pre[maxn];
bool vis[maxn*2];
void solve(){
clr(pre,-1);
clr(ind,0);
clr(vis,0);
clr(dp,0x3f);
spfa(0,d);
spfa(n-1,d2);
for(int i=0;i<m;i++){
Edge &e=egs[i];
if(d[e.u]+1+d2[e.v]==d[n-1]){
// prf("(%d,%d)\n",e.u+1,e.v+1);
G2[e.u].pb(i);
ind[e.v]++;
}
}
//
queue<int> Q;
Q.push(0),dp[0]=0;
while(!Q.empty()){
int u=Q.front(); Q.pop();
rep(i,0,G2[u].sz()){
Edge& e=egs[G2[u][i]];
if(dp[e.v]>dp[u]+(e.z^1)){
dp[e.v]=dp[u]+(e.z^1);
pre[e.v]=G2[u][i];
}
ind[e.v]--;
if(ind[e.v]==0){
Q.push(e.v);
}
}
}
// bug(dp[n-1]);
VI ans;
int p=n-1;
while(p){
Edge& e=egs[pre[p]];
// prf("<%d,%d>\n",e.u,e.v);
if(e.z==0){
ans.pb(pre[p]);
}
vis[pre[p]]=vis[pre[p]^1]=1;
p=e.u;
}
for(int i=0;i<m;i++){
if(vis[i]) continue;
Edge& e=egs[i];
if(e.z==1){
ans.pb(i);
}
vis[i]=vis[i^1]=1;
}
prf("%d\n",ans.sz());
rep(i,0,ans.sz()){
Edge& e=egs[ans[i]];
prf("%d %d %d\n",e.u+1,e.v+1,e.z^1);
}
}
}spfa;
int main() {
int n,m;
scf("%d%d",&n,&m);
spfa.init(n);
for(int i=0;i<m;i++){
int u,v,z;
scf("%d%d%d",&u,&v,&z); u--,v--;
spfa.addEdge(u,v,z);
spfa.addEdge(v,u,z);
}
spfa.solve();
return 0;
}
//end-----------------------------------------------------------------------
其实。。spfa跑最短路的时候就能处理出0最少的最短路径了。。orz..上面的做法好蠢。。
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e5+10;
struct Edge{
int u,v,z;
Edge(int u,int v,int z):u(u),v(v),z(z){}
};
struct Spfa{
int n,m;
vector<Edge> egs;
VI G[maxn];
bool inq[maxn];
int d[maxn],dp[maxn];
int pre[maxn];
void init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
egs.clear();
}
void addEdge(int u,int v,int z){
egs.pb(Edge(u,v,z));
m=egs.sz();
G[u].pb(m-1);
}
int spfa(int s){
clr(pre,-1);
clr(dp,0x3f);
queue<int> Q;
clr(inq,0);
rep(i,0,n) d[i]=INF;
dp[s]=0;
d[s]=0,inq[s]=true,Q.push(s);
while(!Q.empty()){
int u=Q.front(); Q.pop();
inq[u]=false;
for(int i=0;i<G[u].sz();i++){
Edge& e=egs[G[u][i]];
if(d[e.v]>d[u]+1||d[e.v]==d[u]+1&&dp[e.v]>dp[u]+(e.z^1)){
d[e.v]=d[u]+1;
dp[e.v]=dp[u]+(e.z^1);
pre[e.v]=G[u][i];
if(!inq[e.v]){
Q.push(e.v);
inq[e.v]=true;
}
}
}
}
}
bool vis[maxn*2];
void solve(){
clr(vis,0);
spfa(0);
VI ans;
int p=n-1;
while(p){
Edge& e=egs[pre[p]];
if(e.z==0){
ans.pb(pre[p]);
}
vis[pre[p]]=vis[pre[p]^1]=1;
p=e.u;
}
for(int i=0;i<m;i++){
if(vis[i]) continue;
Edge& e=egs[i];
if(e.z==1){
ans.pb(i);
}
vis[i]=vis[i^1]=1;
}
prf("%d\n",ans.sz());
rep(i,0,ans.sz()){
Edge& e=egs[ans[i]];
prf("%d %d %d\n",e.u+1,e.v+1,e.z^1);
}
}
}spfa;
int main() {
int n,m;
scf("%d%d",&n,&m);
spfa.init(n);
for(int i=0;i<m;i++){
int u,v,z;
scf("%d%d%d",&u,&v,&z); u--,v--;
spfa.addEdge(u,v,z);
spfa.addEdge(v,u,z);
}
spfa.solve();
return 0;
}
//end-----------------------------------------------------------------------