| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3447 | Accepted: 1480 |
Description
The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.
"For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."
Would you please tell Mr. M at least how long will it take to reach his sweet home?
Input
The input contains multiple test cases.
The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
The second line contains one integer M (0<=M<=10000), which is the number of roads.
The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.
To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.
Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.
Output
For each test case, output one integer representing the minimum time to reach home.
If it is impossible to reach home according to Mr. M's demands, output -1 instead.
Sample Input
2 1 1 2 100 1 2 3 3 1 2 100 1 3 40 2 3 50 1 2 1 5 5 3 1 200 5 3 150 2 5 160 4 3 170 4 2 170 1 2 2 2 1 0
Sample Output
100 90540
题意:从city1到city2的最短距离,并且该最短路上只能有一条边的两个节点属于不同的group。
解题思路:只能够存在这样的一条特殊边,那就枚举这一条边,假设为(u,v)且u属于group1,v属于group2,然后将该条边拆掉,这样只要算city1->u没有经过group2的点的最短路径,和v->city2没有经过group1的点的最短路径。分析到这里就比较好建图了,将两次最短路,分别以city1和city2为起点,算出到group1和group2内所有点的最短路径。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 605; const int inf = 0x3f3f3f3f; struct Edge { int to,next,w; }edge[20005]; struct Arc //保存输入边的信息 { int u,v,w; }arc[10005]; int n,m,cnt,pre[maxn],camp[maxn]; int dis1[maxn],dis2[maxn]; bool vis[maxn]; void addedge(int u,int v,int w) { edge[cnt].to = v; edge[cnt].next = pre[u]; edge[cnt].w = w; pre[u] = cnt++; } void Dijkstra(int s,int type,int *dis) { int k = s,MIN; memset(vis,false,sizeof(vis)); for(int i = 1; i <= n; i++) dis[i] = inf; dis[s] = 0; for(int i = pre[k]; i != -1; i = edge[i].next) { int v = edge[i].to; if(camp[v] == type) dis[v] = edge[i].w; } vis[k] = true; for(int i = 1; i < n; i++) { MIN = inf; for(int j = 1; j <= n; j++) { if(vis[j] == true || camp[j] != type) continue; if(MIN > dis[j]) { MIN = dis[j]; k = j; } } if(MIN == inf) break; vis[k] = true; for(int j = pre[k]; j != -1; j = edge[j].next) { int v = edge[j].to; if(vis[v] == true || camp[v] != type) continue; if(dis[v] > dis[k] + edge[j].w) dis[v] = dis[k] + edge[j].w; } } } int main() { int u,v,w; while(scanf("%d",&n),n) { scanf("%d",&m); cnt = 0; memset(pre,-1,sizeof(pre)); for(int i = 1; i <= m; i++) { scanf("%d%d%d",&u,&v,&w); arc[i].u = u; arc[i].v = v; arc[i].w = w; addedge(u,v,w); addedge(v,u,w); } for(int i = 1; i <= n; i++) scanf("%d",&camp[i]); Dijkstra(1,camp[1],dis1); Dijkstra(2,camp[2],dis2); int ans = inf; for(int i = 1; i <= m; i++) { u = arc[i].u; v = arc[i].v; w = arc[i].w; if(camp[u] == camp[v]) continue; if(camp[u] == 2) swap(u,v); ans = min(ans,dis1[u] + w + dis2[v]); } if(ans == inf) printf("-1\n"); else printf("%d\n",ans); } return 0; }
1191




被折叠的 条评论
为什么被折叠?



