解題報告說是上下界費用流,我這裡用的也是上下界的方法,不用上下界也是一樣的,把必經過的邊的費用變成-inf,那麼在求最小費用流時程序一定會經過這條邊,同樣起到了限制下界的作用,這樣更好理解~
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define pf push_front
#define X first
#define Y second
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define MC(a,b) memcpy(a,b,sizeof(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
const int maxn = 222;
const int add = 111;
const int head = 220;
const int end = 221;
struct zz
{
int from;
int to;
int c;
int cost;
int id;
}zx;
vector<zz>g[maxn];
int n,m,k;
int d[maxn][maxn];
int way[maxn];
bool inq[maxn];
int back[maxn];
void floyd()
{
for(int k=0;k<=n;k++)
{
for(int i=0;i<=n;i++)
{
if(d[i][k]==inf) continue;
for(int j=0;j<=n;j++)
{
if(d[k][j]==inf) continue;
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
return ;
}
void link(int now,int to,int c,int cost,int bc=0)
{
zx.from=now;zx.to=to;zx.c=c;zx.cost=cost;zx.id=g[zx.to].size();
g[zx.from].pb(zx);
swap(zx.from,zx.to);zx.c=bc;zx.cost=-cost;zx.id=g[zx.to].size()-1;
g[zx.from].pb(zx);
return ;
}
bool spfa()
{
for(int i=0;i<maxn;i++) way[i]=inf;
MM(back,-1);
queue<int>q;
MM(inq,false);
inq[head]=true;
q.push(head);
way[head]=0;
int now,to,temp;
while(!q.empty())
{
now = q.front();
q.pop();
for(int i=0;i<g[now].size();i++)
{
to = g[now][i].to;
if(g[now][i].c>0)
{
temp = way[now]+g[now][i].cost;
if(temp < way[to])
{
way[to]=temp;
back[to]=g[now][i].id;
if(!inq[to])
{
inq[to]=true;
q.push(to);
}
}
}
}
inq[now]=false;
}
return way[end]!=inf;
}
int dfs(int flow=inf,int to = end)
{
if(to == head) return flow;
int now = g[to][back[to]].to;
int id = g[to][back[to]].id;
int temp = dfs(min(g[now][id].c,flow),now);
g[now][id].c-=temp;
g[to][back[to]].c+=temp;
return temp;
}
int ek()
{
int ans=0;
for(int i=0;i<n;i++)
{
ans+=d[i][i+1];
}
ans+=d[0][n];
while(spfa())
{
ans+=dfs()*way[end];
}
return ans;
}
int main()
{
while(cin>>n>>m>>k)
{
if(!m && !n && !k) return 0;
for(int i=0;i<maxn;i++)
{
g[i].clear();
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
d[i][j]=inf;
}
}
for(int i=0;i<=n;i++)
{
d[i][i]=0;
}
int now,to,len;
for(int i=1;i<=m;i++)
{
cin>>now>>to>>len;
d[now][to]=d[to][now]=min(d[now][to],len);
}
floyd();
link(0,1,0,d[0][1],1);
for(int i=2;i<=n;i++)
{
link(0,i,1,d[0][i]);
}
for(int i=1;i<n;i++)
{
link(i+add,end,1,d[i][0]);
}
link(n+add,end,0,d[n][0],1);
link(0,end,inf,0);
link(head,0,k-1,0,1);
for(int i=1;i<=n;i++)
{
link(i+add,i+1,0,d[i][i+1],1);
for(int j=i+2;j<=n;j++)
{
link(i+add,j,1,d[i][j]);
}
}
cout<<ek()<<endl;
}
return 0;
}