题意:无向图,从1到n的所有路径中取路径中最长的那条 比较, 然后输出值最小的那条。
当时想的是直接深搜, 记录当前路径最大值, 到n比较即可。
这样会出问题,就是从别的地方搜过来就会破坏掉原有的顺序,那么记录路径的最大值就会出现问题了。简直被自己蠢哭。
放上自己比赛时的错误代码和错误例子
#include
using namespace std;
struct Node{
int v, val, id;
Node(){}
Node(int _v, int _val, int _id){
v = _v; val = _val; id = _id;
}
};
const int maxn = 1e5 + 10;
const int maxm = 4e5 + 10;
vector vec[maxn];
int vis[maxm]; int ans = 0x3f3f3f3f; int n , m ;
void dfs(int u , int value){
cout << "u = " << u << " " << "value = " << value << endl;
system("pause");
if(u == n) {
ans = min(ans, value); return ;
}
for(int i = 0 ; i < vec[u].size(); i ++){
int v = vec[u][i].v; int val = vec[u][i].val; int id = vec[u][i].id;
if(vis[id]) continue;
vis[id] = 1;
dfs(v, max(value, val));
}
}
int main(){
int count = 0;
freopen("data.out", "r", stdin);
cin >> n >> m; memset(vis, 0, sizeof(vis));
for(int i = 0 ; i < m; i ++){
int a, b , c; cin >> a >> b >> c;
vec[a].push_back(Node(b, c, count ++));
vec[b].push_back(Node(a, c, count ++));
}
dfs(1, 0);
cout << ans << endl;
}
/*
4 6
1 2 1
1 3 2
2 4 2
4 3 4
1 4 3
2 3 3
*/
正解就直接spfa即可, 记录每个点从1到它路径上的最大值, 不断去更新, 输出n点的即可。
#include
#include
#include
#include
using namespace std;
const int oo = 1e9;
const int MAXN = 1e5 + 5;
const int MAXM = 2 * 1e5 + 5;
int N, M;
bool inq[MAXN] = { 0 };
int opt[MAXN];
struct Edge
{
Edge(int _from, int _to, int _weight) :from(_from), to(_to), weight(_weight) {}
int from, to;
int weight;
};
vector ve;
vector<int> v[MAXN];
void bfs(int s)
{
queue<int> q;
q.push(s);
inq[s] = true;
opt[s] = 0;
for (; !q.empty();)
{
int t = q.front();
q.pop();
inq[t] = false;
for (int i = 0; i < v[t].size(); i++)
{
int e = v[t][i];
int next = ve[e].to;
int m;
if ((m = max(opt[t], ve[e].weight)) < opt[next])
{
opt[next] = m;
if (!inq[next])
{
q.push(next);
inq[next] = true;
}
}
}
}
}
int main()
{
// freopen("data.out", "r", stdin);
scanf("%d%d", &N, &M);
fill(opt + 1, opt + N + 1, oo);
int f, t, w;
for (int i = 0; i < M; i++)
{
scanf("%d%d%d", &f, &t, &w);
ve.push_back(Edge(f, t, w));
ve.push_back(Edge(t, f, w));
v[f].push_back(ve.size() - 2);
v[t].push_back(ve.size() - 1);
}
bfs(1);
cout << opt[N] << endl;
return 0;
}