Life is a journey, and the road we travel has twists and turns, which sometimes lead us to unexpected places and unexpected people.
Now our journey of Dalian ends. To be carefully considered are the following questions.
Next month in Xian, an essential lesson which we must be present had been scheduled.
But before the lesson, we need to attend a wedding in Shanghai.
We are not willing to pass through a city twice.
All available expressways between cities are known.
What we require is the shortest path, from Dalian to Xian, passing through Shanghai.
Here we go.
Input Format
There are several test cases.
The first line of input contains an integer t which is the total number of test cases.
For each test case, the first line contains an integer m (m≤10000) which is the number of known expressways.
Each of the following m lines describes an expressway which contains two string indicating the names of two cities and an integer indicating the length of the expressway.
The expressway connects two given cities and it is bidirectional.
Output Format
For eact test case, output the shortest path from Dalian to Xian, passing through Shanghai, or output −1if it does not exist.
样例输入
3 2 Dalian Shanghai 3 Shanghai Xian 4 5 Dalian Shanghai 7 Shanghai Nanjing 1 Dalian Nanjing 3 Nanjing Xian 5 Shanghai Xian 8 3 Dalian Nanjing 6 Shanghai Nanjing 7 Nanjing Xian 8
样例输出
7 12 -1
题目来源
题意:
找出最短路,从大连出发,经过上海到西安,且一个城市只能经过一次。
POINT:
最小费用最大流,拆点,出点和入点,s连接大连和西安,t为上海的出点,上海的入点和出点cap为2,其余的为1.
当最大流是2的时候输出最小费用,不是2则走不到。
#include <iostream>
#include <string.h>
#include <map>
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
map<string,int> mp;
const int maxn = 20000+5;
const int inf = 0x3f3f3f3f;
int num;
int s,t;
struct edge
{
int from,to,flow,cap,cost;
edge(int u,int v,int f,int c,int co):
from(u),to(v),flow(f),cap(c),cost(co){}
};
vector<edge>len;
vector<int>G[maxn];
void add(int u,int v,int cap,int cost)
{
len.push_back(edge(u,v,0,cap,cost));
len.push_back(edge(v,u,0,0,-cost));
G[u].push_back(len.size()-2);
G[v].push_back(len.size()-1);
}
int inq[maxn],dis[maxn],pre[maxn];
bool spfa(int &f,int &c)
{
for(int i=0;i<=2*num;i++) dis[i]=inf,pre[i]=0;
queue<int>q;
dis[s]=0;
inq[s]=1;q.push(s);
int a=inf;
while(!q.empty())
{
int u=q.front();q.pop();
inq[u]=0;
for(int i=0;i<G[u].size();i++)
{
edge e=len[G[u][i]];
if(dis[e.to]>dis[u]+e.cost&&e.cap>e.flow)
{
dis[e.to]=dis[u]+e.cost;
pre[e.to]=G[u][i];
a=min(a,e.cap-e.flow);
if(inq[e.to]==0)
{
q.push(e.to);
inq[e.to]=1;
}
}
}
}
if(dis[t]==inf) return 0;
int u=t;
f+=a;
c+=a*dis[t];
while(u!=s)
{
len[pre[u]].flow+=a;
len[pre[u]^1].flow-=a;
u=len[pre[u]].from;
}
return 1;
}
int mincostmaxflow()
{
int cost=0,flow=0;
while(spfa(flow,cost))
{
continue;
}
if(flow==2) printf("%d\n",cost);
else printf("-1\n");
}
void init()
{
num=0;
mp.clear();
len.clear();
for(int i=0;i<maxn;i++) G[i].clear();
}
int Xian,Shanghai,Dalian;
int name(string ss)
{
if(mp[ss]==0)
{
mp[ss]=++num;
}
if(ss=="Xian") Xian=mp[ss];
if(ss=="Shanghai") Shanghai=mp[ss];
if(ss=="Dalian") Dalian=mp[ss];
return mp[ss];
}
int main()
{
//每个城市标号x,出边为2x,入边为2x-1;
int T;
scanf("%d",&T);
while(T--)
{
init();
int m;
scanf("%d",&m);
while(m--)
{
string s1,s2;
int w;
cin>>s1>>s2>>w;
int u=name(s1);
int v=name(s2);
add(2*u,2*v-1,1,w);
add(2*v,2*u-1,1,w);
}
for(int i=1;i<=num;i++)
{
if(i==Shanghai)
add(2*i-1,2*i,2,0);
else
add(2*i-1,2*i,1,0);
}
add(0,Xian*2-1,1,0);
add(0,Dalian*2-1,1,0);
s=0,t=Shanghai*2;
mincostmaxflow();
}
return 0;
}