1. 程式人生 > >ACM ICPC 烏魯木齊網路賽 J. Our Journey of Dalian Ends

ACM ICPC 烏魯木齊網路賽 J. Our Journey of Dalian Ends

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 tt which is the total number of test cases.


For each test case, the first line contains an integer m~(m\le 10000)m (m≤10000) which is the number of known expressways.


Each of the following mm 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 -1−1 if 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

每個城市拆成出點和入點,源點連西安和大連,匯點連上海,相當於求從西安到上海和從大連到上海最小距離之和,每個城市入點和出點之間連一條容量為1的邊,但是注意,上海的容量必須是2,再根據給出的邊,分別連接出點入點,存入相應花費,那麼問題就可以轉化成最小費用最大流了,如果流量不為2輸出-1,否則輸出最小花費。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<map>
#include<queue>
#include<string>
using namespace std;
#define ll long long
const ll maxm = 10005;
const ll INF = 1e18 + 7;
struct node
{
	ll u, v, flow, cost, next;
}edge[maxm * 10];
map<string, ll>p;
ll cnt, s, t, n, m, sum, FLOW;
ll head[maxm * 10], dis[maxm * 10], pre[maxm * 10];
char a[maxm], b[maxm];
void init()
{
	p.clear();
	cnt = 0, s = 0, t = n * 5 + 1, sum = 0, FLOW = 0;
	memset(head, -1, sizeof(head));
}
void add(ll u, ll v, ll flow, ll cost)
{
	edge[cnt].u = u, edge[cnt].v = v;
	edge[cnt].flow = flow, edge[cnt].cost = cost;
	edge[cnt].next = head[u], head[u] = cnt++;
	edge[cnt].u = v, edge[cnt].v = u;
	edge[cnt].flow = 0, edge[cnt].cost = -cost;
	edge[cnt].next = head[v], head[v] = cnt++;
}
ll bfs()
{
	queue<ll>q;
	for (ll i = 0;i <= t;i++) dis[i] = INF;
	memset(pre, -1, sizeof(pre));
	dis[s] = 0, q.push(s);
	ll rev = 0;
	while (!q.empty())
	{
		ll u = q.front();q.pop();
		for (ll i = head[u];i != -1;i = edge[i].next)
		{
			ll v = edge[i].v;
			if (dis[v] > dis[u] + edge[i].cost&&edge[i].flow)
			{
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i, q.push(v);
			}
		}
	}
	if (dis[t] == INF) return 0;
	return 1;
}
ll MCMF()
{
	ll ans = 0, minflow;
	while (bfs())
	{
		minflow = INF;
		for (ll i = pre[t];i != -1;i = pre[edge[i].u])
			minflow = min(minflow, edge[i].flow);
		for (ll i = pre[t];i != -1;i = pre[edge[i].u])
			edge[i].flow -= minflow, edge[i ^ 1].flow += minflow;
		ans += dis[t] * minflow;
		FLOW += minflow;
	}
	return ans;
}
int main()
{
	ll i, j, k, T, c;
	scanf("%lld", &T);
	while (T--)
	{
		scanf("%lld", &n);
		init();
		ll nn = n * 2;
		for (i = 1;i <= n;i++)
		{
			scanf("%s%s%lld", a, b, &c);
			if (p[a] == 0)
			{
				p[a] = ++sum, k = 1;
				if (strcmp(a, "Shanghai") == 0) k = 2;
				add(p[a], p[a] + nn, k, 0);
			}
			if (p[b] == 0)
			{
				p[b] = ++sum, k = 1;
				if (strcmp(b, "Shanghai") == 0) k = 2;
				add(p[b], p[b] + nn, k, 0);
			}
			ll u = p[a], v = p[b];
			add(u + nn, v, INF, c);
			add(v + nn, u, INF, c);
		}
		ll u = p["Dalian"];
		add(s, u, 1, 0);
		u = p["Xian"];
		add(s, u, 1, 0);
		u = p["Shanghai"];
		add(u + nn, t, 2, 0);
		ll ans = MCMF();
		if (FLOW == 2) printf("%lld\n", ans);
		else printf("-1\n");
	}
	return 0;
}


相關推薦

ACM ICPC 烏魯木齊網路 J. Our Journey of Dalian Ends

Life is a journey, and the road we travel has twists and turns, which sometimes lead us to unexpected places and unexpected people. Now o

2017 ACM-ICPC 亞洲區(烏魯木齊賽區)網路 J.Our Journey of Dalian Ends【最小費用最大流】

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 Dal

J: Our Journey of Dalian Ends

urumqi-2017-online 存一個學姐用的模板(:з」∠) 因為每個點只能經過一次,拆點,限制為1 以上海為起點 大連和西安兩個點再連向終點,限制為1 判斷最終流量是否為2就能判斷是否從大連開始到西安結束了 #include<bi

計蒜客-2017 ACM-ICPC 亞洲區(烏魯木齊賽區)網路JOur Journey of Dalian Ends (最小費用最大流)

題意: 給定若干個城市,出發點為大連,目的地為西安,但是要求中途必須經過上海,並且圖中每個城市只能經過一次,給出m條路(雙向道路),走第i條路需要wi代價,求所有滿足要求的方案中花費的最小代價,如果沒有滿足的方案,輸出-1。 思路: 相當於求從大連到上海加上西安到上海花費的

2017烏魯木齊網絡 JOur Journey of Dalian Ends ( 最小費用最大流 )

增廣路 ali += ase turn src eof weight flow 題目鏈接 題意 : 給出一副圖,大連是起點,終點是西安,要求你求出從起點到終點且經過中轉點上海的最小花費是多少? 分析 : 最短路是最小費用最大流的一個特例,所以有些包含中轉限制或者經過點

Our Journey of Dalian Ends

給定雙向邊,求Dalian到Shanghai再到Xian,途中不經過統一的城市,求最短距離 費用流 源點往Shanghai連邊,城市進行拆點處理(流量為1,確保只經過一次),然後城市之間流量為1,費用為對應距離,最後Dalian和Xian與匯點連邊

The 2018 ACM-ICPC上海大都會 J Beautiful Numbers (數位DP)

mes div spa ems urn 余數 limit style 狀態 題意:求小於等於N且能被自己所有位上數之和整除的數的個數。 分析:裸的數位dp。用一個三位數組dp[i][j][k]記錄:第i位,之前數位之和為j,對某個mod余數為k的狀態下滿足條件的個數。這裏m

Traveling on the Axis (ACM-ICPC青島網路

BaoBao is taking a walk in the interval [0,n] on the number axis, but he is not free to move, as at every point (i−0.5) for all i∈[

計蒜客 Transport Ship (2018-ACM-ICPC-焦作-網路)DP

There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry the weight of V[i]V[i] and the number of

2016 acm/icpc 青島網路 題解(hdu 5878-5889,9道題)

5878.I Count Two Three(打表預處理,二分) 題目大意:有一些數可以寫成2a3b5c7d的形式,稱之為”I count two three numbers”.輸入一個數,問比他大的最小的”I count two three number

2017 ACM-ICPC西安現場 J題LOL(暴力)

題意:輸入五個長度為100的01串,從每個串中選擇一個是1的位置,任意兩個串中選擇的位置不能相同,一共有多少種情況,答案乘以常數531192758再對1e9+7取模。 如果對五個串都dfs複雜度是10^10穩T,所以對前四個串dfs,最後一個串的貢獻直接加到最終的答案上。

2018 ACM-ICPC南京網路 Magical Girl Haze(分層最短路)

There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici​. Haz

2017 ACM 區域青島站(現場) K Our Journey of Xian Ends

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 Xia

【分層最短路】dijkstra講解+複習 /2018 ACM-ICPC南京網路 Magical Girl Haze

首先,HDU-2544 來回顧一下dij這個東西(連結: 這裡) 講解: (來源:坐在馬桶上學演算法) 不斷對邊進行鬆弛 (以下為copy) 演算法的基本思想是:每次找到離源點(上面例子的源點就是 1 號頂點)最近的一個頂點,然後以該頂點為中心進行擴充套件,最終得到

【分層圖/ 變形最短路】2018 ACM-ICPC 南京網路

題意: 給出一張有向圖,能夠把最多k條邊的權值變為零,問從點1到點n的最短距離是多少。 題解一: 分層圖。 建立k張一模一樣的有向圖,層與層之間用權值為零的邊相連。 層與層之間的跳躍就是代表選擇了一條邊權值變為零。 跑一遍迪傑斯特拉,輸出最後一層的點n距離就是

2018 ACM-ICPC北京網路 A.Saving Tang Monk II(bfs)

好久沒bfs了,沒想到用三維陣列去標記狀態來進行bfs。。。但仔細一想想,這麼解很有道理。。。 因為這個圖每個格子可以走多遍,我們考慮,對於一個格子,如果帶著相同的氧氣瓶走兩次,那結果是相同的。所以我們從這個約束進行搜尋,開一個三位數字,vis[i][j][k]代表帶著k個

2018 ACM-ICPC 瀋陽網路C Convex hull

考慮杜教篩就跑偏了… 定義gay(i)=i2∗μ2(i)gay(i)=i2∗μ2(i) ∑ni=1∑ij=1gay(i)=∑ni=1(n−i+1)gay(i)=∑ni=1(n−i+1)i2μ2(i)∑i=1n∑j=1igay(i)=∑i=1n(n−i+1

2017 icpc 青島站 K. Our Journey of Xian Ends 最小費用流

K. Our Journey of Xian Ends 題意:這英文題真是比六級閱讀還難讀,給你一個無向圖,求西安-->上海-->青島-->上海的最短路,每個城市有一個機場,但是上海有兩個機場 虹橋機場和浦東機場,上海這兩個機場可以互達(你可以從西安到浦東,然後再從虹橋到青

2017 icpc 青島站 K. Our Journey of Xian Ends 最小費用流

題意:這英文題真是比六級閱讀還難讀,給你一個無向圖,求西安-->上海-->青島-->上海的最短路,每個城市有一個機場,但是上海有兩個機場 虹橋機場和浦東機場,上海這兩個機場可以互達(你可以從西安到浦東,然後再從虹橋到青島,距離為0),每個機場只能下飛機和

Our Journey of Xian Ends

ron using one 圖片 map gin anti def cos Our Journey of Xian Ends 鏈接:here 參考http://blog.csdn.net/wangshuhe963/article/details/78516821 費