看懂题意后第一反应用dijkstra,然后亲测可行
过程和dijkstra类似,只是要改一些细节
每次找到未扩展的点中的载重最大的点,对它进行扩展
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
int street[1100][1100];//邻接矩阵
int theMaxWeight[1100];//从起点到达某个路口的最大载重
bool flag[1100];
int main()
{
//freopen("in.txt","r",stdin);
int total;
cin>>total;
int k=1;
while (total--)
{
int n,m;
scanf("%d %d",&n,&m);
memset(street,0,sizeof(street));
while (m--)
{
int a,b,val;
scanf("%d %d %d",&a,&b,&val);
if(val>street[a][b])
{
street[a][b]=val;
street[b][a]=val;//因为这句忘了加我调了2个小时。。。。。
}
}
memset(flag,false,sizeof(flag));//一开始都没有操作过
memset(theMaxWeight,0,sizeof(theMaxWeight));
theMaxWeight[1]=INT_MAX;
while (true)
{
int maxi=-1;
for (int i = 1; i <=n; i++)//找到未扩展过的点中的载重最大的点
{
if(flag[i])
{
continue;
}
if(maxi==-1||theMaxWeight[i]>theMaxWeight[maxi])
{
maxi=i;
}
}
if(maxi==-1)
{
break;
}
flag[maxi]=true;
for (int i = 1; i <=n ; i++)//对该点进行扩展
{
if(theMaxWeight[maxi]<street[maxi][i])
{
if(theMaxWeight[maxi]>theMaxWeight[i])
{
theMaxWeight[i]=theMaxWeight[maxi];
}
}
else
{
if(street[maxi][i]>theMaxWeight[i])
{
theMaxWeight[i]=street[maxi][i];
}
}
}
}
printf("City #%d:\n",k++);
printf("%d\n\n",theMaxWeight[n]);
}
return 0;
}