题意:给n个地点及许多边,现在问你要用最短时间内有多少种不相交的最短路
思路:因为要的是最短路上的边,所以先跑最短路将满足的边全部拿出来建网络流的图,然后在跑个最大流就行了因为一条边只能用一次,所以网络流就行
#include <queue>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=3010;
struct edge{
int to,cap,rev;
edge(){}
edge(int a,int b,int c){to=a;cap=b;rev=c;}
};
vector<edge> G[maxn];
int level[maxn],iter[maxn];
void add_edge(int from,int to,int cap){
G[from].push_back(edge(to,cap,G[to].size()));
G[to].push_back(edge(from,0,G[from].size()-1));
}
void bfs(int s){
memset(level,-1,sizeof(level));
queue<int>que;
level[s]=0;que.push(s);
while(!que.empty()){
int v=que.front();que.pop();
for(unsigned int i=0;i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&level[e.to]<0){
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}
int dfs(int v,int t,int f){
if(v==t) return f;
for(int &i=iter[v];i<G[v].size();i++){
edge &e=G[v][i];
if(e.cap>0&&level[v]<level[e.to]){
int d=dfs(e.to,t,min(f,e.cap));
if(d>0){
e.cap-=d;
G[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t){
int flow=0;
while(1){
bfs(s);
if(level[t]<0) return flow;
memset(iter,0,sizeof(iter));
int f;
while((f=dfs(s,t,inf))>0) flow+=f;
}
}
struct edge1{
int to,cost;
edge1(int a,int b){to=a;cost=b;}
};
typedef pair<int,int>P;
vector<edge1>G1[maxn];
int dis[maxn];
void dijkstra(int s){
priority_queue<P,vector<P>,greater<P> >que;
fill(dis,dis+maxn,inf);
dis[s]=0;que.push(P(0,s));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(dis[v]<p.first) continue;
for(unsigned int i=0;i<G1[v].size();i++){
edge1 e=G1[v][i];
if(dis[e.to]>dis[v]+e.cost){
dis[e.to]=dis[v]+e.cost;
que.push(P(dis[e.to],e.to));
}
}
}
}
int A[1000010],B[1000010],C[1000010];
int main(){
int T,n;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<maxn;i++) G[i].clear();
for(int i=0;i<maxn;i++) G1[i].clear();
add_edge(0,1,inf);
add_edge(n,n+1,inf);
int k=0;
while(1){
scanf("%d%d%d",&A[k],&B[k],&C[k]);
if(A[k]==0&&B[k]==0&&C[k]==0) break;
G1[A[k]].push_back(edge1(B[k],C[k]));
G1[B[k]].push_back(edge1(A[k],C[k]));
k++;
}
dijkstra(1);
if(n==1||dis[n]==inf){
printf("0\n");continue;
}
for(int i=0;i<maxn;i++) G1[i].clear();
for(int i=0;i<k;i++){
if(dis[A[i]]>dis[B[i]]) swap(A[i],B[i]);
if(dis[B[i]]-dis[A[i]]==C[i]){
add_edge(A[i],B[i],1);
}
}
int ans=max_flow(0,n+1);
printf("%d\n",ans);
}
return 0;
}