传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2923
题意:(百度的)3个数字n,c,r。n表示城市数;c表示第二行有r+1个城市,第一个城市是源点,接下来几个是目标点;后面还有r行,表示路线信息(包括起点、终点、方向、距离)。
工作是从源点到某个目标点(pass这个目标点)再返回(一次只能pass一个目标点)。这里还有一个坑,就是如果一个目标点在第二行出现两次,那么这个目标点应该pass两次。输出所走过的最短路。
用STL函数真香,我以后一定要抽时间学一下常用的STL,真香~~
AC代码:
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
int chart[101][101];
void Floyd(int n)
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
chart[i][j]=min(chart[i][k]+chart[k][j],chart[i][j]);
}
}
}
}
int main()
{
int cnt=0;
int n,r,c,num;
char s1[101],s2[101],from,to;
int f[1001];
map<string,int> mp;
while(~scanf("%d%d%d",&n,&c,&r)&&n)
{
memset(chart,INF,sizeof(chart));
mp.clear();
int k=1;
for(int i=0;i<=c;i++)
{
scanf("%s",s1);
if(!mp[s1]) mp[s1]=k++;
f[i]=mp[s1];
}
while(r--)
{
scanf("%s %c-%d-%c %s", s1, &from, &num, &to, s2);
if(!mp[s1]) mp[s1]=k++;
if(!mp[s2]) mp[s2]=k++;
int x=mp[s1];
int y=mp[s2];
if(to=='>' && chart[x][y]>num)
chart[x][y]=num;
if(from=='<' && chart[y][x]>num)
chart[y][x]=num;
}
Floyd(n);
num=0;
for(int i=1;i<=c;i++)
num+=chart[1][f[i]]+chart[f[i]][1];
printf("%d. %d\n",++cnt,num) ;
}
return 0;
}