东风夜放花千树
东风夜放花千树
#include <vector> using namespace std; class dpair; bool line[601][601],vis[601]; int con_y[601]; bool dfs(int x, vector<vector<double>>& distance) { double now = 541881452.0; for (int j = 0; j < 600; j++) { if (line[x][j] && !vis[j]) vis[j] = true; if ((con_y[j] == -1 || dfs(con_y[j], distance)) && (distance[x][j] <= now)) { now = distance[x][j]; con_y[j] = x; return true; } } return false; } void match(vector<vector<double>>& distance) { memset(con_y, -1, sizeof(con_y)); for (int i = 0; i < 600; i++) dfs(i, distance); } vector<dpair> arrange(vector<vector<double>>& distance) { vector<dpair> output; output.resize(601); /* dpair 中含有两个变量 dronerank targetrank 创建一个名字为 a 的 dpair 变量用法为 dpair a(i,j) i,j为相应编号 // output[a] = dpair{1,2} 支持== !=运算 也可以直接dpair(i,j) 分别为无人机的编号 和目标点编号 output为最终返回数组 用法和普通数组一样 规模为800 distance为二维数组,distance[i][j] 为编号为i的无人机与编号为j的目标点的距离 */ /*下方写算法*/ match(distance); for (int i = 0; i < 600; i++) { for (int j = i; j < 600; j++) { if (line[i][j] == true) output[i] = dpair(i,j); } } /*到此为止*/ return output; }
#include <cstdio> #include <cstring> #include <algorithm> #include "numerical_vector.h" const int MAXN = 1000 + 5; using namespace std; int n, m, e, x, y, ans; int con_y[MAXN]; bool vis[MAXN], line[MAXN][MAXN]; class drone { public: vector }; pair<int,int> dfs(int x) { for (int j = 1; j <= m; j++) { if (line[x][j] && !vis[j]) { vis[j] = true; if (con_y[j] == -1 || dfs(con_y[j])) { con_y[j] = x; return make_pair{ x,j }; } } } return make_pair{ -1,0 }; } bool reach(int a, int b) { if (drone[a][X] == drone[b][X] && drone[a][Y] == drone[b][Y] && drone[a][Z] == drone[b][Z]) } void solu(int a, int b)//由index a移动向index b { double dis = sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y) + (b.z - a.z) * (b.z - a.z)); while(reach(a,b)) } void match() { memset(con_y, -1, sizeof(con_y)); for (int i = 1; i <= n; i++) { memset(vis, false, sizeof(vis)); pair<int, int> p;//first - index1 ; second - index2 p = dfs(i); solu(p.first, p.second); } } int main() { scanf("%lf%lf%lf", &n, &m, &e); for (int i = 1; i <= e; i++) { scanf("%lf%lf", &x, &y); if (x >= 1 && y >= 1 && x <= n && y <= m) line[x][y] = true; } match(); printf("%d", ans); return 0; }
#pragma once #include <vector> #include <iostream> namespace mine { #define X 1 #define Y 2 #define Z 3 class vector { public: typedef double number_type; typedef std::vector<number_type> data_type; private: typedef const vector& pvector; private: data_type data; size_t dimensionality; public: vector(); vector(const std::initializer_list<number_type>&); vector(const size_t&); vector(pvector); vector(const data_type&); ~vector(); public: vector operator=(pvector); friend vector operator+(pvector, pvector); friend vector operator-(pvector, pvector); friend number_type operator*(pvector, pvector); friend vector operator*(const number_type&, pvector); friend vector operator*(pvector, const number_type&); friend vector operator/(pvector, const number_type&); vector operator+=(pvector); vector operator-=(pvector); vector operator*=(pvector) = delete; vector operator*=(const number_type&); vector operator/=(const number_type&); friend bool operator==(pvector, pvector); friend bool operator!=(pvector, pvector); const number_type& operator[](const size_t&)const; number_type& operator[](const size_t&); friend std::ostream& operator<<(std::ostream&, pvector); public: inline size_t dim() { return dimensionality; } number_type norm(); friend number_type distance(pvector, pvector); }; vector::vector() { dimensionality = 0; } vector::vector(const size_t& dim) { dimensionality = dim; data.resize(dim, 0); } vector::vector(pvector val) { data = val.data; dimensionality = val.dimensionality; } vector::vector(const data_type& input) { data = input; dimensionality = data.size(); } vector::vector(const std::initializer_list<number_type>& list) { dimensionality = list.size(); data.clear(); for (std::initializer_list<number_type>::iterator i = list.begin(); i != list.end(); i++) data.push_back(*i); } vector::~vector() { data.~vector(); } vector vector::operator=(pvector val) { dimensionality = val.dimensionality; data = val.data; return val; } vector operator+(vector::pvector a, vector::pvector b) { vector output; output.data.resize(max(a.dimensionality, b.dimensionality), 0); output.dimensionality = output.data.size(); for (size_t i = 0; i < output.dimensionality; i++) { if (i < a.dimensionality) output.data[i] += a.data[i]; if (i < b.dimensionality) output.data[i] += b.data[i]; } return output; } vector operator-(vector::pvector a, vector::pvector b) { vector output; output.data.resize(max(a.dimensionality, b.dimensionality), 0); output.dimensionality = output.data.size(); for (size_t i = 0; i < output.dimensionality; i++) { if (i < a.dimensionality) output.data[i] += a.data[i]; if (i < b.dimensionality) output.data[i] -= b.data[i]; } return output; } vector::number_type operator*(vector::pvector a, vector::pvector b) { vector::number_type output = 0; size_t mindim = min(a.dimensionality, b.dimensionality); for (size_t i = 0; i < mindim; i++) output += a.data[i] * b.data[i]; return output; } vector operator*(vector::pvector val, const vector::number_type& mult) { vector output = val; for (size_t i = 0; i < output.dimensionality; i++) output.data[i] *= mult; return output; } vector operator/(vector::pvector val, const vector::number_type& div) { vector output = val; for (size_t i = 0; i < output.dimensionality; i++) output.data[i] /= div; return output; } vector operator*(const vector::number_type& mult, vector::pvector val) { vector output = val; for (size_t i = 0; i < output.dimensionality; i++) output.data[i] *= mult; return output; } vector vector::operator+=(pvector val) { if (val.dimensionality > dimensionality) { dimensionality = val.dimensionality; data.resize(dimensionality); } for (size_t i = 0; i < val.dimensionality; i++) data[i] += val.data[i]; return *this; } vector vector::operator-=(pvector val) { if (val.dimensionality > dimensionality) { dimensionality = val.dimensionality; data.resize(dimensionality); } for (size_t i = 0; i < val.dimensionality; i++) data[i] -= val.data[i]; return *this; } vector vector::operator*=(const number_type& mult) { for (size_t i = 0; i < dimensionality; i++) data[i] *= mult; return *this; } vector vector::operator/=(const number_type& div) { for (size_t i = 0; i < dimensionality; i++) data[i] /= div; return *this; } bool operator==(vector::pvector a, vector::pvector b) { if (a.dimensionality != b.dimensionality) return false; for (size_t i = 0; i < a.dimensionality; i++) if (a.data[i] != b.data[i])return false; return true; } bool operator!=(vector::pvector a, vector::pvector b) { if (a.dimensionality != b.dimensionality) return true; for (size_t i = 0; i < a.dimensionality; i++) if (a.data[i] != b.data[i])return true; return false; } const vector::number_type& vector::operator[](const size_t& pos)const { if (pos > dimensionality) throw std::logic_error("out of range"); return data[pos - 1]; } vector::number_type& vector::operator[](const size_t& pos) { if (pos > dimensionality) throw std::logic_error("out of range"); return data[pos - 1]; } std::ostream& operator<<(std::ostream& out, vector::pvector val) { out << '('; size_t end = val.dimensionality - 1; for (size_t i = 0; i < end; i++) out << val.data[i] << ", "; out << val.data[val.dimensionality - 1] << ')'; return out; } /****************************************************/ vector::number_type vector::norm() { number_type output = 0; for (size_t i = 0; i < dimensionality; i++) output += data[i] * data[i]; output = std::sqrt(output); return output; } vector::number_type distance(vector::pvector a, vector::pvector b) { vector::number_type output = 0; size_t maxdim = max(a.dimensionality, b.dimensionality); vector::number_type temp; for (size_t i = 0; i < maxdim; i++) { if (i < a.dimensionality) temp = a.data[i]; else temp = 0; if (i < b.dimensionality) temp -= b.data[i]; output += temp * temp; output = std::sqrt(output); return output; } }
const int inf = 1e9; int n,m,s,t; int u,v,w; int cnt = -1; //为了 ^ 的找反向边做准备 int max_flow; int dep[MAXN]; //记录每一个节点的深度 int head[MAXN]; //存图 int cur[MAXN]; //记录路径的 queue<int> q; struct Node { int len; int next; int to; }edge[MAXN * 10 << 1]; inline void add_edge(int u,int v,int w) //加边建图 { edge[++cnt].to = v; edge[cnt].len = w; edge[cnt].next = head[u]; head[u] = cnt; } inline bool bfs(int s,int t) //判断是不是可以跑 { memset(dep,0x7f,sizeof(dep)); while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) cur[i] = head[i]; //初始化 dep[s] = 0; //起点开始 q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(int i=head[u];i;i=edge[i].next) { int v = edge[i].to; if(dep[v] > inf && edge[i].len) //正向边,可以跑 { dep[v] = dep[u] + 1; q.push(v); } } } if(dep[t] < inf) return true; else return false; } inline int dfs(int now,int t,int limit) /mit就是一个限制 //dfs的优点:在一次增广的过程中,寻找多条增广的路径 { if(!limit || now == t) return limit; int flow = 0; int f; for(int i=head[now];i;i=edge[i].next) { cur[now] = i; //i是边的编号,更改流道的路径 int v = edge[i].to; if(dep[v] == dep[now] + 1 && (f = dfs(v,t,min(limit,edge[i].len)))) { flow += f; limit -= f; edge[i].len -= f; edge[i ^ 1].len += f; if(!limit) break; } } return flow; } void Dinic(int s,int t) { while(bfs(s,t)) max_flow += dfs(s,t,inf); }
bool vis[MAXN]; int n,m,s,t; int x,y,z,f; int dis[MAXN],pre[MAXN],last[MAXN],flow[MAXN]; int maxflow,mincost; struct Node { int to; int next; int flow; int dis; }edge[MAXN<<1]; int head[MAXN[,num_edge; queue<int> q; void add_edge(int u,int v,int flow,int dis) { edge[++num_edge].next = head[u]; edge[num_edge].to = v; edge[num_edge].flow = flow; edge[num_edge].dis = dis; head[u] = num_edge; } void SPFA(int s,int t) { memset(dis,0x7f,sizeof(dis)); memset(flow,0x7f,sizeof(flow)); memset(vis,0,sizeof(vis)); q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1; while (!q.empty()) { int now = q.front(); q.pop(); vis[now] = 0; for(int i=head[now];i!=-1;i=edge[i].next) { if(edge[i].flow && dis[edge[i].to] > dis[now] + edge[i].dis) { dis[edge[i].to] = dis[now]+edge[i].dis; pre[edge[i].to] = now; last[edge[i].to] = i; flow[edge[i].to] = min(flow[now],edge[i].flow); if(!vis[edge[i].to]) { vis[edge[i].to] = 1; q.push(edge[i].to); } } } } } void MCMF() { while(SPFA(s,t)) { int now = t; maxflow += flow[t]; mincost += flow[t] * dis[t]; while(now != s) { edge[last[now]].flow -= flow; edge[last[now] ^ 1].flow += flow; now = pre[now]; } } } int main() { memset(head,-1,sizeof(head)); num_edge = -1; scanf("%d%d%d%d",&n,&m,&s,&t); for(int i=1;i<=m;i++) { scanf("%d%d%d%d",&x,&y,&z,&f); add_edge(x,y,z,f); add_edge(y,x,0,-f); } MCMF(); }