传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=5294
多校第一场第7题,上午Codeforces坏了,改改多校玩一玩,这道是板子题,当时我没读懂题意,哎,当时是真心菜啊!!!
题意:
无向图N个点M条边,输出最少删掉几条边破坏最短路,最多删掉几条边不破坏最短路。
最多删几条边不破坏最短路的答案是M减去最短路里边数最少的那个,最少删掉几条边破坏最短路的做法是把所有在最短路上的边重新建图,变成流量为1的网络流,求最小割。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
const int INF = 100000000;
const int MOD = 1e9 + 7;
const int N = 2000 + 10;
const int MAXM = 120000 + 10;
int n, m;
struct Node {
int from, to, next;
int cap;
} edge[MAXM];
struct Edge {
int v, w;
Edge(int v, int w): v(v), w(w) {}
};
int tol;
int head[N];
int dep[N];
int gap[N];
vector<Edge> G[N],E[N];
void init() {
tol = 0;
memset(head, -1, sizeof(head));
for(int i=0;i<n;i++){
E[i].clear();
}
}
void addedge(int u, int v, int w) {
edge[tol].from = u;
edge[tol].to = v;
edge[tol].cap = w;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].from = v;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
void BFS(int start, int end) {
memset(dep, -1, sizeof(dep));
memset(gap, 0, sizeof(gap));
gap[0] = 1;
int que[N];
int front, rear;
front = rear = 0;
dep[end] = 0;
que[rear++] = end;
while(front != rear) {
int u = que[front++];
if(front == N)front = 0;
for(int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if(dep[v] != -1)continue;
que[rear++] = v;
if(rear == N)rear = 0;
dep[v] = dep[u] + 1;
++gap[dep[v]];
}
}
}
int SAP(int start, int end) {
int res = 0;
BFS(start, end);
int cur[N];
int S[N];
int top = 0;
memcpy(cur, head, sizeof(head));
int u = start;
int i;
while(dep[start] < n) {
if(u == end) {
int temp = INF;
int inser;
for(i = 0; i < top; i++)
if(temp > edge[S[i]].cap) {
temp = edge[S[i]].cap;
inser = i;
}
for(i = 0; i < top; i++) {
edge[S[i]].cap -= temp;
edge[S[i] ^ 1].cap += temp;
}
res += temp;
top = inser;
u = edge[S[top]].from;
}
if(u != end && gap[dep[u] - 1] == 0) //出现断层,无增广路
break;
for(i = cur[u]; i != -1; i = edge[i].next)
if(edge[i].cap != 0 && dep[u] == dep[edge[i].to] + 1)
break;
if(i != -1) {
cur[u] = i;
S[top++] = i;
u = edge[i].to;
} else {
int min = n;
for(i = head[u]; i != -1; i = edge[i].next) {
if(edge[i].cap == 0)continue;
if(min > dep[edge[i].to]) {
min = dep[edge[i].to];
cur[u] = i;
}
}
--gap[dep[u]];
dep[u] = min + 1;
++gap[dep[u]];
if(u != start)u = edge[S[--top]].from;
}
}
return res;
}
int dis[N], cnt[N];
bool vis[N];
void Dijkstra(int s, int dist[],vector<Edge>G[]) {
priority_queue<pair<int, int> > Q;
for(int i = 0; i < n; i++)
dist[i] = INF, cnt[i] = INF;
memset(vis, 0, sizeof(vis));
Q.push(make_pair(0, s));
dist[s] = 0;
cnt[s] = 0;
while(!Q.empty()) {
int u = Q.top().second;
Q.pop();
if(vis[u]) continue ;
vis[u] = 1;
for(int i = 0; i < G[u].size(); i++) {
Edge& e = G[u][i];
if(dist[u] + e.w < dist[e.v]) {
dist[e.v] = dist[u] + e.w;
Q.push(make_pair(-dist[e.v], e.v)); // 默认大的元素优先级高,所以要取最小就加负号
}
}
}
}
int main() {
while(scanf("%d%d", &n, &m) != EOF) {
int x, y, t;
for(int i = 0; i < n; i++) {
G[i].clear();
}
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &x, &y, &t);
x--, y--;
G[x].push_back(Edge(y, t));
G[y].push_back(Edge(x, t));
}
Dijkstra(0, dis, G);
init();
for(int u = 0; u < n; u++) {
for(int i = 0; i < G[u].size(); i++) {
int v = G[u][i].v, w = G[u][i].w;
if(dis[u] + w == dis[v]) {
addedge(u, v, 1);
E[u].push_back(Edge(v, 1));
}
}
}
Dijkstra(0, dis, E);
printf("%d %d\n", SAP(0, n - 1), m - dis[n - 1]);
}
return 0;
}