求起点到终点的某条路径长度减去其中一条边的最小值。
依次删掉每条边,然后求起点到终点的最短路,最小的那个就是答案。
还有就是map要记得清空,我在这里WA了一次。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
map<string,int> mp;
struct Edge
{
int from,to,cost;
void set(int a,int b,int c)
{
from=a;
to=b;
cost=c;
}
};
int main()
{
string st,ed;
while(cin>>st>>ed)
{
mp.clear();
int m,n=0,N=0;
cin>>m;
Edge p[205];
string a,b;
int w;
for(int i=0; i<m; ++i)
{
cin>>a>>b>>w;
if(!mp[a]) mp[a]=++n;
if(!mp[b]) mp[b]=++n;
int aa=mp[a],bb=mp[b];
p[N++].set(aa,bb,w);
p[N++].set(bb,aa,w);
}
int dist[105];
memset(dist,0x7f,sizeof(dist));
int ans=dist[0];
int las=mp[ed];
for(int i=0; i<N; ++i)
{
int tmp=p[i].cost;
p[i].cost=0;
memset(dist,0x7f,sizeof(dist));
dist[mp[st]]=0;
while(1)
{
bool update=false;
for(int j=0; j<N; ++j)
{
int to=p[j].to,from=p[j].from,v=p[j].cost;
if(dist[to]>dist[from]+v)
{
dist[to]=dist[from]+v;
update=true;
}
}
if(!update) break;
}
ans=min(dist[las],ans);
p[i].cost=tmp;
}
printf("%d\n",ans);
}
return 0;
}