Aizu 2249 & cf 449B

1、Aizu - 2249

选的边肯定是最短路上的。

如果一个点有多个入度,取价值最小的。

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(x) (int)x.size()
#define de(x) cout<< #x<<" = "<<x<<endl
#define dd(x) cout<< #x<<" = "<<x<<" "
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi; const int N=101010;
int n,m;
int cc[N];
ll dis[N];
bool vis[N];
vector<pii> g[N];
pair<pii, pii> e[N]; void spfa() {
vi q;q.pb(1);
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[1]=0;vis[1]=1;
rep(i,0,sz(q)) {
int u=q[i];vis[u]=0;
rep(j,0,sz(g[u])) {
int v=g[u][j].fi, w=g[u][j].se;
if(dis[v]>dis[u]+w) {
dis[v]=dis[u]+w;
if(!vis[v]) {
vis[v]=1;
q.pb(v);
}
}
}
}
} int main() {
while(~scanf("%d%d",&n,&m)) {
if(n==0&&m==0) break;
///init
rep(i,0,n+1) g[i].clear();
///read
rep(i,1,m+1) {
int u,v,w,c;scanf("%d%d%d%d",&u,&v,&w,&c);
g[u].pb(mp(v, w));
g[v].pb(mp(u, w));
e[i]=mp(mp(u, v), mp(w, c));
}
///solve
spfa();
memset(cc,0x3f,sizeof(cc));
rep(i,1,m+1) {
int u=e[i].fi.fi, v=e[i].fi.se;
int w=e[i].se.fi, c=e[i].se.se;
if(dis[u]+w==dis[v]) cc[v]=min(cc[v], c);
if(dis[v]+w==dis[u]) cc[u]=min(cc[u], c);
}
int ans=0;
rep(i,2,n+1) ans+=cc[i];
printf("%d\n",ans);
}
return 0;
}

2、cf 449B

官方题解:http://codeforces.com/blog/entry/13112

05-11 19:40