Age of MoyuTime Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 616 Accepted Submission(s): 142 Problem Description Mr.Quin love fishes so much and Mr.Quin’s city has a nautical system,consisiting of N ports and M shipping lines. The ports are numbered 1 to N . Each line is occupied by a Weitian. Each Weitian has an identification number.
Input There might be multiple test cases, no more than 20 . You need to read till the end of input.
Output For each test case output the minimum required cost. If Mr.Quin can’t travel to port N , output −1 instead.
Sample Input 3 3 1 2 1 1 3 2 2 3 1 2 0 3 2 1 2 1 2 3 2
Sample Output 1 -1 2
Source |
题意:
给你n个城市,m条边,有个人想从城市1开始走到城市n,但是每条边都有个对应的公司掌管,如果你走的路换了一个不同的公司,那么你的费用要+1,问从1走到n的最小费用(两个城市间最多只有一条路,从公司1到公司2再到公司1也还要加上一次费用)
做法1:直接暴力做,用set存下走到这个结点费用最少的有哪几个公司,跑一边最短路就好了。
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int maxn=100010;
const int inf=0x3f3f3f3f;
typedef long long ll;
typedef pair<ll,ll> pii;
set<ll> app[maxn];
int head[maxn],cnt,n,m;
ll ans[maxn];
priority_queue<pii> q;
struct node{
int to,next,com;
}e[maxn<<2];
void add(int u,int v,int c){
e[cnt].to=v,e[cnt].next=head[u];
e[cnt].com=c,head[u]=cnt++;
}
void init(){
cnt=0;
for(int i=0;i<=n;i++){
app[i].clear();
head[i]=-1;
ans[i]=inf;
}
}
void bfs(){
while(!q.empty()){
int now=q.top().second,dis=-q.top().first;
q.pop();
if(ans[now]>dis) continue;
for(int i=head[now];~i;i=e[i].next){
int to=e[i].to,nowcom=e[i].com;
int newdis=dis+(!app[now].count(nowcom));
if(ans[to]>newdis){
ans[to]=newdis;
q.push(pii(-newdis,to));
app[to].clear();
app[to].insert(nowcom);
}
else if(ans[to]==newdis){
app[to].insert(nowcom);
}
}
}
}
int main(){
int x,y,z;
while(~scanf("%d%d",&n,&m)){
init();
for(int i=0;i<m;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z); add(y,x,z);
}
ans[1]=0; q.push(pii(0,1));
bfs();
printf("%d\n",ans[n]>=inf?-1:ans[n]);
}
return 0;
}
做法2:拆边!!
将边(pi,qi,ci)拆为(pi,ci)和(qi,ci)两个点,在pi、(pi,ci),qi、(qi,ci),(pi,ci)、(qi,ci)之间连边,并将权值分别赋为1,1,0,意思就是在pi上车需要1元,在pi,qi之间坐车消耗0元,在qi下车又需要1元。
但是这个题的数据比较大,又是多组样例,所以好像怎么做都是T...心累累,所以接下来的代码是过不了的,但是这个做法我觉得很好,想保存下来。请看到的宝宝不要介意。。。
代码如下:注意!!过不了
#include<bits/stdc++.h>
using namespace std;
const int maxn=300005;
const int inf=0x3f3f3f3f;
typedef pair<int,int> pii;
int dist[maxn],vis[maxn],n,m,cnt;
vector<pii> edge[maxn];
map<pii, int > mp;
int gainp(int city,int com){
if(!mp[pii(city,com)]) mp[pii(city,com)]=++cnt;
return mp[pii(city,com)];
}
void add(int u,int v,int c){
edge[u].push_back(pii(v,c));
edge[v].push_back(pii(u,c));
}
void spfa(int st){
for(int i=1;i<=cnt;i++)
dist[i]=inf,vis[i]=0;
queue<int> q;
q.push(st);
vis[st]=1; dist[st]=0;
while(!q.empty()){
int u=q.front(); q.pop();
vis[u]=0;
for(int i=0;i<edge[u].size();i++){
int v=edge[u][i].first,w=edge[u][i].second;
if(dist[v]>dist[u]+w){
dist[v]=dist[u]+w;
if(!vis[v]) {
q.push(v);
vis[v]=1;
}
}
}
}
}
int main(){
int u,v,c;
while(~scanf("%d%d",&n,&m)){
mp.clear();
for(int i=0;i<=maxn;i++)
edge[i].clear();
cnt=n;
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&c);
int uu=gainp(u,c),vv=gainp(v,c);
add(u,uu,1); add(uu,vv,0); add(vv,v,1);
}
spfa(1);
printf("%d\n",dist[n]>=inf?-1:dist[n]/2);
}
return 0;
}