题目概述
城市中有N个地点,编号1到N,你在1号开了个工厂,要把货送到N号的市场,城市中有M条双向路,每条路有负重上限,你每次运的货重量不能超过途中任何一段路的负重上限,问每次最多可运多少货
时限
3000ms/9000ms
输入
第一行正整数times,其后times组数据,每组数据第一行两个正整数N,M,其后M行,每行三个正整数,代表一条路连接的两个地点及其负重上限
限制
1<=N<=1000;1<=负重上限<=1000000
输出
每组数据输出在两行中,第一行
Scenario #A:
其中A为数据序数,从1开始,下一行一个数,为所求最多运货量,每组输出后带一空行
样例输入
1
3 3
1 2 3
1 3 4
2 3 5
样例输出
Scenario #1:
4
讨论
图论,单源最短路的变形(估计最小生成树也能做),bellman_ford队列优化算法,首先起点到自己肯定是无穷承重,从一个点到其他点的时候要么被两点之间的路卡,要么被到这个点之前的路卡,从二者中选一个小的,如此处理所有点直到终点
实现方面没有要说的,题目就这么直白,额特地尝试了一下动态邻接表,静态邻接表,邻接矩阵的效率,都会贴出来,但是注释只给静态邻接表的,毕竟都差不多
题解状态
静态邻接表:1376K,313MS,C++,1259B
邻接矩阵:4132K,422MS,C++,1005B
动态邻接表:1484K,985MS,C++,1183B
题解代码
静态邻接表
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1003
#define memset0(a) memset(a,0,sizeof(a))
struct Edge//边的结构 实际上完全可以拆分成三个数组
{
int to, w, next;
Edge() {}
Edge(int to, int w, int next) :to(to), w(w), next(next) {}
}graph[MAXN * MAXN];//测试数据里有一张完全图 边数是平方级的(不到平方)
int N, M;//地点数 路数
int al[MAXN], index, dis[MAXN];//adjacency_list 邻接表 用来存放每个起点的最后一个点的位置 和之前略有不同 index 为节点分配下标 distance 最短距离 这里是最大的最大负重
queue<int>q;//queue 辅助队列
bool inq[MAXN];//in_queue 地点在队列中
int fun()
{
index = 1;//为了把0空出来 从1开始
for (int p = 0; p < M; p++) {
int a, b, w;
scanf("%d%d%d", &a, &b, &w);//input
graph[index] = Edge(b, w, al[a]);
al[a] = index++;//这东西和栈一样 先塞进去的点在遍历时最后遇到
graph[index] = Edge(a, w, al[b]);
al[b] = index++;
}
for (int p = 1; p <= N; p++)
dis[p] = -INF;
dis[1] = INF;//下面就是算法主体了
q.push(1);
inq[1] = 1;
while (!q.empty()) {
int a = q.front();
q.pop();
inq[a] = 0;
for (int p = al[a]; p; p = graph[p].next) {//还是说 由于类似栈 因而第一个点的next是0 0是空的 作为遍历终点正好
if (dis[graph[p].to] < min(dis[a], graph[p].w)) {
dis[graph[p].to] = min(dis[a], graph[p].w);
if (!inq[graph[p].to]) {
q.push(graph[p].to);
inq[graph[p].to] = 1;
}
}
}
}
return dis[N];
}
int main(void)
{
//freopen("vs_cin.txt", "r", stdin);
//freopen("vs_cout.txt", "w", stdout);
int times;
scanf("%d", ×);//input
for (int p = 1; p <= times; p++) {
scanf("%d%d", &N, &M);//input
printf("Scenario #%d:\n%d\n\n", p, fun());//output
memset0(al);//很可惜这个没法顺手清零
}
}
邻接矩阵:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1003
#define memset0(a) memset(a,0,sizeof(a))
int N, M;
int graph[MAXN][MAXN], dis[MAXN];
queue<int>q;
bool inq[MAXN];
int fun()
{
for (int p = 0; p < M; p++) {
int a, b, w;
scanf("%d%d%d", &a, &b, &w);//input
graph[a][b] = graph[b][a] = w;
}
for (int p = 2; p <= N; p++)
dis[p] = -INF;
dis[1] = INF;
q.push(1);
inq[1] = 1;
while (!q.empty()) {
int a = q.front();
q.pop();
inq[a] = 0;
for (int p = 1; p <= N; p++) {
if (graph[a][p] && dis[p] < min(dis[a], graph[a][p])) {
dis[p] = min(dis[a], graph[a][p]);
if (!inq[p]) {
q.push(p);
inq[p] = 1;
}
}
}
}
return dis[N];
}
int main(void)
{
//freopen("vs_cin.txt", "r", stdin);
//freopen("vs_cout.txt", "w", stdout);
int times;
scanf("%d", ×);//input
for (int p = 1; p <= times; p++) {
scanf("%d%d", &N, &M);//input
printf("Scenario #%d:\n%d\n\n", p, fun());//output
memset0(graph);
}
}
动态邻接表:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1003
#define memset0(a) memset(a,0,sizeof(a))
struct Edge
{
int to, w;
Edge() {}
Edge(int to, int w) :to(to), w(w) {}
};
int N, M;
int dis[MAXN];
vector<vector<Edge>>graph;
queue<int>q;
bool inq[MAXN];
int fun()
{
graph.resize(N + 1);
for (int p = 0; p < M; p++) {
int a, b, w;
scanf("%d%d%d", &a, &b, &w);//input
graph[a].push_back(Edge(b, w));
graph[b].push_back(Edge(a, w));
}
for (int p = 1; p <= N; p++)
dis[p] = 0;
dis[1] = INF;
q.push(1);
inq[1] = 1;
while (!q.empty()) {
int a = q.front();
q.pop();
inq[a] = 0;
for (int p = 0; p < graph[a].size(); p++) {
Edge b = graph[a][p];
if (dis[b.to] < min(dis[a], b.w)) {
dis[b.to] = min(dis[a], b.w);
if (!inq[b.to]) {
q.push(b.to);
inq[b.to] = 1;
}
}
}
}
return dis[N];
}
int main(void)
{
//freopen("vs_cin.txt", "r", stdin);
//freopen("vs_cout.txt", "w", stdout);
int times;
scanf("%d", ×);
for (int p = 1; p <= times; p++) {
scanf("%d%d", &N, &M);//input
printf("Scenario #%d:\n%d\n\n", p, fun());//output
graph.clear();
}
}
EOF