1.新年好(求一次到达多个点的最优解)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 50010, M = 200010, INF = 0x3f3f3f3f;
int n, m;
int h[N], e[M], w[M], ne[M], idx;
int q[N], dist[6][N];
int source[6];
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void dijkstra(int start, int dist[])
{
memset(dist, 0x3f, N * 4);
dist[start] = 0;
memset(st, 0, sizeof st);
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({0, start});
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[ver] + w[i])
{
dist[j] = dist[ver] + w[i];
heap.push({dist[j], j});
}
}
}
}
int dfs(int u, int start, int distance)
{
if (u > 5) return distance;//由于有一个起点,那么就剩五个点可以遍历,固当u>5时返回
int res = INF;
for (int i = 1; i <= 5; i ++ )
if (!st[i])
{
int next = source[i];
st[i] = true;
res = min(res, dfs(u + 1, i, distance + dist[start][next]));
st[i] = false;
}
return res;
}
int main()
{
scanf("%d%d", &n, &m);
source[0] = 1;
for (int i = 1; i <= 5; i ++ ) scanf("%d", &source[i]);
memset(h, -1, sizeof h);
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
for (int i = 0; i < 6; i ++ ) dijkstra(source[i], dist[i]);//求出每个亲戚距离其他每家的最短距离
memset(st, 0, sizeof st);
printf("%d\n", dfs(1, 0, 0));//全排列,求出最优解
return 0;
}
2.通信线路(二分法求出所有路径上第k大的数,并求最小数)
代码看不懂可以在纸上画一个二分图就很好想了
#include <cstring>
#include <iostream>
#include <algorithm>
#include <deque>
using namespace std;
const int N = 1010, M = 20010;
int n, m, k;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
deque<int> q;
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
bool check(int bound)
{
memset(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
q.push_back(1);
dist[1] = 0;
while (q.size())
{
int t = q.front();
q.pop_front();
if (st[t]) continue;
st[t] = true;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i], x = w[i] > bound;
if (dist[j] > dist[t] + x)
{
dist[j] = dist[t] + x;
if (!x) q.push_front(j);
else q.push_back(j);
}
}
}
return dist[n] <= k;
}
int main()
{
cin >> n >> m >> k;
memset(h, -1, sizeof h);
while (m -- )
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c), add(b, a, c);
}
int l = 0, r = 1e6 + 1;
while (l < r)//二分查找最合适的距离(查的是距离)
{
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}//最后求得的一定是最短距离
if (r == 1e6 + 1) cout << -1 << endl;
else cout << r << endl;
return 0;
}
3.道路与航线(拓扑+dijkstra)
//题目中给出了此图不是拓扑图,因此可以用拓扑算法来求(保证了至少有一个块的入度为零,并且如果这个块内没有生产点时,那么就没有方案可以到达该连通块。因为我们要从生产点到达各个城市,而不是从城市到达生产点)
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef pair<int,int> PII;
const int N = 25010, M = 150010, INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], w[M], idx;
int dist[N];
bool st[N];
int n, mr, mp, s;
//id:点所在的连通块号, bcnt:连通块数量
int id[N], bcnt;
//连通块的入度数组
int deg[N];
vector<int> block[N];
int q[N], hh, tt = -1;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u)
{
block[bcnt].push_back(u);
id[u] = bcnt;
for(int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if(!id[j]) dfs(j);
}
}
void dijkstra(int block_id)//只有从生产点开始时每个连通块才开始遍历,在此之前遍历的都没用
{
priority_queue<PII, vector<PII>, greater<PII>> heap;
//取出该连通块所有的点, 入堆
for(int u : block[block_id])
{
heap.push({dist[u], u});
}
while(heap.size())
{
PII t = heap.top();
heap.pop();
int u = t.second;
if(st[u]) continue;
st[u] = true;
for(int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[u] + w[i])
{
dist[j] = dist[u] + w[i];
if(id[j] == block_id) heap.push({dist[j], j});
}
if(id[j] != block_id && --deg[id[j]] == 0) q[++tt] = id[j];//当一个块成零时入队
}
}
}
void topoSort()
{
//dist赋初值
memset(dist, 0x3f, sizeof dist);
dist[s] = 0;
//入度为0的连通块入队列
for(int i = 1; i <= bcnt; i++)
{
if(!deg[i]) q[++tt] = i;
}
while(hh <= tt)
{
int t = q[hh++];
dijkstra(t);
}
}
int main()
{
scanf("%d%d%d%d", &n, &mr, &mp, &s);
memset(h, -1, sizeof h);
for(int i = 0; i < mr; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
//建立所有的连通块
for(int i = 1; i <= n; i++)
{
if(!id[i]) {
++bcnt;
dfs(i);
}
}
//在连通块之间建边,也就是航线
for(int i = 0; i < mp; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), deg[id[b]]++;
}
//算法开始
topoSort();
for(int i = 1; i <= n; i++)
{
if(dist[i] > INF / 2) puts("NO PATH");
else printf("%d\n", dist[i]);
}
return 0;
}
4.最优贸易
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010, M = 2000010;
int n, m;
int w[N];
int hs[N], ht[N], e[M], ne[M], idx;//hs[N]表示正向边,即从起点到终点的边,ht[N]为反向边,即从终点到起点的边,其输入的是反向的
int dmin[N], dmax[N];
int q[N];
bool st[N];
void add(int h[], int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void spfa(int h[], int dist[], int type)
{
int hh = 0, tt = 1;
if (type == 0)
{
memset(dist, 0x3f, sizeof dmin);
dist[1] = w[1];
q[0] = 1;
}
else
{
memset(dist, -0x3f, sizeof dmax);
dist[n] = w[n];
q[0] = n;
}
while (hh != tt)
{
int t = q[hh ++ ];
if (hh == N) hh = 0;//循环队列,出队后再从起点开始
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (type == 0 && dist[j] > min(dist[t], w[j]) || type == 1 && dist[j] < max(dist[t], w[j]))
{
if (type == 0) dist[j] = min(dist[t], w[j]);
else dist[j] = max(dist[t], w[j]);
if (!st[j])
{
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
memset(hs, -1, sizeof hs);
memset(ht, -1, sizeof ht);
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(hs, a, b), add(ht, b, a);
if (c == 2) add(hs, b, a), add(ht, a, b);
}
spfa(hs, dmin, 0);
spfa(ht, dmax, 1);
int res = 0;
for (int i = 1; i <= n; i ++ ) res = max(res, dmax[i] - dmin[i]);
printf("%d\n", res);
return 0;
}